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 · 1 comment

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!

@kquick
Copy link
Member

kquick commented Dec 13, 2024

I had intentionally not provided the ability to pass arguments to Actor constructors due to several concerns about supporting this. I'll provide an overview of the concerns here preparatory to adding them to the main documentation.

  • Remote Actors
    When actors are created on remote systems, the arguments to the constructor would need to be passed to that remote system, which implies the ability to marshal, transmit, and reconstitute those arguments by the local and remote Admin, which adds complexities and associated concerns to the Admin.

  • Initialization argument longevity.
    Admins will restart failed Actors, so the initialization values would need to be stable for the potential lifetime of the Actor System.

  • Actor initialization activities
    Another concern is the activities performed by the actor with these initialization arguments. At constructor time, the Actor is not fully established as a functioning Actor, and therefore does not have the normal message generation and response capabilities yet. Thus, there would need to be a lot of documentation and possibly active protection against a constructor sending messages or doing other interactions with other elements.

As an alternative, have you had a chance to look at the initmsgs helper (http://thespianpy.com/doc/using.html#h:5f80e194-ab38-406e-b077-984496b37073 and https://github.com/thespianpy/Thespian/blob/master/thespian/initmsgs.py); it does not provide constructor arguments, but it does handle the "initialization via messages" aspect of an Actor.

Let me know your thoughts based on the above concerns.

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

2 participants