Thespian Actor Framework

repository·master·Indexed 18 days ago

https://github.com/thespianpy/thespian

An actor-based framework for Python providing distributed computing capabilities, including actor management, automatic scaling, and environment orchestration. Features include Actor Troupes for parallel operations, multi-system conventions for distributed communication, fault tolerance with atomic messaging, and dynamic capability-based deployment. Supports loadable sources for updating actor code without restarting the system.

Tokens
4.3K
Snippets
14
Records
28
Agent score
63%

What's inside Thespian

  1. What is the Thespian Actor Library?

    master
    Thespian is a framework implementing the Actor model for Python applications. It provides the infrastructure necessary for building systems composed of independent, communicating entities called Actors. It was originally developed at GoDaddy to manage large-scale server environments and has since been open-sourced for general use.
  2. Avoid periodic failures in Actor-based applications

    master

    The Act 2 example demonstrates a pattern to solve periodic failures (such as missing updates or unexpected actor creation) that occur during startup handshakes.

    Instead of using non-deterministic delays (like time.sleep()) or making actors ephemeral, this example uses a message-passing pattern:

    Include the destination address (e.g., the address of an Analyzer) directly within the request message (e.g., EncodeThis). This allows the sender to provide the routing information immediately. The system will automatically delay the message until the target actor is ready, ensuring a deterministic startup without manual synchronization.

  3. Understand Actor hierarchy and termination behavior

    master

    In Thespian, killing a parent Actor causes all of its child Actors to be killed as well.

    If you kill the primary entry point Actor (e.g., the Acceptor), all downstream child processes (like Analyzer or Encoder actors) will also be terminated. Because the globalName is no longer associated with a running process, the next time the application is invoked, Thespian will create a completely new instance of the Actor hierarchy.

  4. Configure Multi-System Actor Environments with Capabilities

    master

    Thespian supports multi-system configurations where multiple Actor Systems run independently (often on different network nodes). You can define specific capabilities for each system to control which Actors run where.

    Actors can specify their requirements using the @requireCapability syntax. An Actor will only run in a system that possesses a matching set of capabilities. This allows for:

    • Targeted Deployment: Ensuring specific encoders (e.g., Morse, Base64, Rot13) run on systems equipped with the necessary hardware or software.
    • Fault Tolerance and Failover: By creating systems with overlapping capabilities, if one system fails, Actors can be automatically recreated in another system that meets their requirements.

    In this example setup, systems communicate via an administrator port. The first system started on port 1900 acts as the convention leader.

    # Example of starting systems with specific capabilities
    $ python start.py 1900
    $ python start.py 10000 "morse,Caesar cipher"
    $ python start.py 10101 "64bit encoder"
  5. Handle child actor failures with ChildActorExited messages

    master

    In Thespian, when a child actor exits, its parent actor receives a ChildActorExited message. You can use this message to perform cleanup or state management, such as removing the exited child's address from a tracking dictionary. This allows the parent to detect the failure and restart the child when a new request arrives.

    Additionally, when an actor is being killed, it receives an ActorExitRequest message. You can intercept this message within your actor to perform shutdown or cleanup operations before the actor terminates.

  6. Design for fault tolerance using atomic messaging

    master

    To build resilient, self-healing Actor-based applications, favor atomic messaging.

    Instead of relying on an Actor's internal state to hold partial information for a multi-step request, pass all the information required to process a request within a single message. This ensures that if an actor (like an Analyzer) is killed and restarted, the new instance can immediately process subsequent requests using the information provided in the message, without needing to recover lost state.

  7. Use loadable sources for dynamic code updates

    master

    Thespian supports a 'loadable source' feature that allows you to update running Actor code without restarting the Actor System. This is achieved by loading a ZIP file containing updated source code into a running system.

    Workflow

    1. Create a source package: Package your updated Python sources into a .zip file.
    2. Load the sources: Use a loading mechanism to inject the ZIP into a running Actor System. This returns a unique source hash.
    3. Instantiate Actors: When calling createActor(), pass the returned source hash to ensure the Actor is instantiated from that specific version of the code.

    Key Concepts

    • Source Authority: To support dynamic loading, a special Actor must register itself as the Source Authority. When new sources are loaded, they are passed to this Actor for validation (e.g., checking digital signatures) or decryption. Thespian only allows Actor creation from a source once the Source Authority has validated it.
    • Side-by-side Versions: Multiple versions of sources can exist in a system simultaneously. Old source hashes continue to work, allowing old Actors to finish processing requests while new requests are directed to new versions. This enables zero-downtime updates.
    • Automatic Remote Distribution: If you load sources into one Actor System, those sources are automatically propagated to any remote Actor System that attempts to create an Actor using that specific source hash.
    # Conceptual usage when creating an actor from a specific loaded source
    # The source_hash is obtained from the load operation
    actor_ref = system.createActor(MyActorClass, name='my_actor', sourceHash='a3ad084f02e92c6a2e3eb70e5d72a6c2')
  8. How Thespian Multi-System Conventions work

    master

    Multiple Thespian systems can communicate as part of a "convention". In this architecture, a Convention Leader handles all registration, resulting in a star topology for the convention.

    Convention management is handled entirely by Thespian, meaning it has little to no impact on standard Actor development. However, Actors can optionally participate in Convention-related events.

  9. Manage Actor System Lifecycle and Capabilities

    master

    You can manage the lifecycle of Actor Systems and their capabilities dynamically without restarting the entire application.

    Starting Systems

    Use start.py with the following arguments:

    1. port: The port number for the system's administrator.
    2. capabilities: A comma-separated list (no whitespace) of capabilities to assign to the system.

    Stopping Systems

    Use stop.py to shut down specific Actor Systems. By default, it targets port 1900. You can pass additional port numbers to shut down multiple systems.

    Dynamic Capability Updates

    You can add or remove capabilities from a running system using a utility like chgcap.py. When a capability is removed, Thespian re-checks all running Actors in that system. If an Actor's @requireCapability specification is no longer met by the system's new capability set, the Actor is automatically shut down.

    chgcap.py usage: python chgcap.py <port> <+/-> <"capability name">

    # Start a system on port 10000 with specific capabilities
    $ python start.py 10000 "morse,Caesar cipher"
    
    # Add a capability to system 10101
    $ python chgcap.py 10101 + morse
    
    # Remove a capability from system 10101
    $ python chgcap.py 10101 - "64bit encoder"
    
    # Stop multiple systems
    $ python stop.py 1900 10000 10101
  10. Run the Multi-System Example

    master

    The example consists of three main lifecycle scripts: start.py to initialize the Actor System, app.py to execute the application logic, and stop.py to clean up.

    When running these scripts, you can provide an optional first argument to specify the system base (e.g., multiprocTCPBase or multiprocUDPBase).

    # 1. Start the Actor System (defaults to multiprocTCPBase)
    python start.py multiprocUDPBase
    
    # 2. Run the application (pass message via stdin)
    echo "Hello, World!" | python app.py multiprocUDPBase
    
    # 3. Stop the Actor System and all Actors
    python stop.py multiprocUDPBase