Skip to content

Cooperating actors

This example shows how multiple actors can be set up to cooperate with each other by passing either ActorRef or ActorProxy instances to actors, either at setup time as arguments to start() or later through messages.

Example

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

import pykka


class Adder(pykka.ThreadingActor):
    def add_one(self, i: int) -> int:
        print(f"Adder is adding 1 to {i}")
        return i + 1


class Bookkeeper(pykka.ThreadingActor):
    def __init__(self, adder: pykka.ActorProxy[Adder]) -> None:
        super().__init__()
        self.adder = adder

    def count_to(self, target: int) -> None:
        i = 0
        while i < target:
            i = self.adder.add_one(i).get()
            print(f"Bookkeeper got {i} back")


if __name__ == "__main__":
    # Start the adder actor
    adder = Adder.start().proxy()

    # Start the bookkeeper actor, passing it the adder actor's proxy
    bookkeeper = Bookkeeper.start(adder).proxy()

    # Ask the bookkeeper to count to 5
    bookkeeper.count_to(5).get()

    # Stop all running actors using the ActorRegistry
    pykka.ActorRegistry.stop_all()

Output

$ uv run examples/cooperation.py
Adder is adding 1 to 0
Bookkeeper got 1 back
Adder is adding 1 to 1
Bookkeeper got 2 back
Adder is adding 1 to 2
Bookkeeper got 3 back
Adder is adding 1 to 3
Bookkeeper got 4 back
Adder is adding 1 to 4
Bookkeeper got 5 back