Skip to content

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

@rossoha

Description

@rossoha

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions