Skip to content

Basic actor

This example shows:

  • how to define an actor by creating a class that subclasses ThreadingActor and defines the on_receive() method,
  • how the actor can store internal state, here done by modifying self._stored_messages,
  • how to start and stop an actor, using start() and stop(), and
  • how to interact with the actor from the outside, using tell() and ask().

Example

examples/basic.py
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "pykka",
# ]
# ///

from typing import Any

import pykka

# Define a unique message to request stored messages
GetMessages = object()


class BasicActor(pykka.ThreadingActor):
    def __init__(self) -> None:
        super().__init__()
        self._stored_messages: list[Any] = []

    def on_receive(self, message: Any) -> Any:
        if message is GetMessages:
            return self._stored_messages
        self._stored_messages.append(message)
        return None


if __name__ == "__main__":
    # Start the actor
    actor_ref = BasicActor.start()

    # Send some messages to the actor
    actor_ref.tell({"no": "Norway", "se": "Sweden"})
    actor_ref.tell({"a": 3, "b": 4, "c": 5})

    # Retrieve and print stored messages
    print(actor_ref.ask(GetMessages))

    # Stop the actor
    actor_ref.stop()

Output

$ uv run examples/basic.py
[{'no': 'Norway', 'se': 'Sweden'}, {'a': 3, 'b': 4, 'c': 5}]