Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add Support for Constructor Arguments in createActor Method #76

Open
rossoha opened this issue Oct 15, 2024 · 0 comments

Comments

@rossoha
Copy link

rossoha commented Oct 15, 2024

Hello Thespian team,

I’m working with Thespian (version 3.10) and found a limitation that could simplify actor initialization. When creating an actor using createActor, it’s currently not possible to pass arguments directly to the actor’s constructor (__init__). Instead, initialization often needs to be handled via messages after the actor is created or by manually setting defaults and checking for uninitialized values in receiveMessage.

Current Approach

For actors that require specific initialization values (like configuration parameters or dependencies), we are forced to handle this in one of two ways:

  1. Send an initialization message immediately after creating the actor.
  2. Set default values and manually reinitialize the actor upon receiving its first message.

This can be cumbersome for actors with complex initialization requirements.

Request

I propose adding support for passing constructor arguments (e.g., via initArgs) when calling createActor. This would enable more seamless actor creation by allowing the following:

actor_ref = actor_system.createActor(MyActor, initArgs={"arg1": "value1", "arg2": "value2"})

Internally, the actor’s __init__ method would be called with the supplied arguments (arg1, arg2, etc.), allowing for direct actor initialization.

This approach is similar to the withPossibleInitArgs utility that selectively passes arguments to the constructor based on the class’s __init__ signature.

Example Implementation

Here’s a similar concept using withPossibleInitArgs for class initialization:

class withPossibleInitArgs(object):
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def create(self, klass):
        initsig = inspect.signature(klass.__init__).parameters
        return klass(**{k: self.kwargs[k] for k in initsig if k in self.kwargs})

Benefits:

  • Cleaner Initialization: By directly supporting constructor arguments, actors can be initialized in a cleaner, more Pythonic manner without needing to send an extra message for initialization.
  • Improved Flexibility: This would allow actors to receive dependencies or configurations at creation time, simplifying the process for both developers and the system.

Is this feature already on the roadmap, or would it be something you’d consider adding in future versions?

Thanks for your consideration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant