Pykka

raw JSON →
4.4.2 verified Fri May 01 auth: no python

Pykka is a Python implementation of the actor model, providing concurrency primitives for building distributed and concurrent systems. Version 4.4.2 runs on Python >=3.10. New releases are sporadic.

pip install pykka
error ModuleNotFoundError: No module named 'pykka'
cause Pykka not installed or wrong Python environment.
fix
Install with 'pip install pykka' and verify Python version >=3.10.
error AttributeError: 'MyActor' object has no attribute 'start'
cause Attempting to call start() on an instance instead of the class.
fix
Use MyActor.start() (class method) not my_actor.start().
error TimeoutError: Actor call timed out after ... seconds
cause Actor's on_receive never returned a value (or deadlocked).
fix
Ensure every on_receive returns a result. If no result, call actor.stop() and return a default.
gotcha Actors are not threadsafe; avoid sharing mutable state between actors. Use message passing only.
fix Design actors as isolated units communicating via messages (dicts or immutable objects).
gotcha Unlike the standard library, Pykka actors must be stopped explicitly to release resources. ActorRegistry will not clean them up automatically.
fix Call actor.stop() or use a context manager: with Greeter.start() as actor: ...
breaking In Pykka 4.x, on_receive must return a value or raise an exception; previously None was allowed and would cause hangs.
fix Ensure on_receive always returns a value (or call actor.stop() explicitly without a return).
gotcha Proxy() and get() are blocking. Using them inside an actor's on_receive can cause deadlocks.
fix Use future.get() only outside actors, or use call() with a callback instead.

Define an actor class, start it, send a message, and stop.

import pykka

class Greeter(pykka.ThreadingActor):
    def __init__(self, greeting='Hi'):
        super().__init__()
        self.greeting = greeting

    def on_receive(self, message):
        return f'{self.greeting}, {message}'

actor = Greeter.start(greeting='Hello').proxy()
print(actor.on_receive('World').get())  # Hello, World
actor.stop()