actors is a small actor framework for in-process asynchronous message passing in Python.
- Low overhead. Can run any number of actors on a single thread.
- Built-in supervision hierarchy and fault tolerance.
- Extensible with dispatchers and executors.
Install from PyPI using
$ pip install actors
from actors import Actor, ActorSystem class Greeter(Actor): def receive(self, message): print("Hello %s" % message) with ActorSystem() as system: greeter = system.actor_of(Greeter) greeter.tell("world")
Creating the actor system
ActorSystem is responsible for creating, configuring and managing a group of actors,
dispatchers and other parts of the actor framework. Normally one system should be created per
system.terminate() is called (or the context manager exit) all actors are
To create an actor, extend the
Actor base class and implement
receive, then use
actor_of factory method spawn an actor instance.
To pass constructor arguments to the actor, use
from functools import partial class Greeter(Actor): def __init__(self, format): self._format = format ... greeter = system.actor_of(partial(Greeter, "Hello %s!")
Actor base class additionally provides hooks for specializing lifetime and supervisor
behaviour. See API documentation for details.
Messages can be sent by invoking
tell on an
ActorRef which will send one-way message (also
known as “fire-and-forget”), or with
ask which returns a
Future` with a possible reply.
In both cases, messages are sent asynchronously.
from actors.utils.ask import ask # send a one-way message greeter.tell("Hello!") # get a future with a reply reply = ask(greeter, "Hello?") # block until the reply is ready answer = reply.get()
When sending messages using
tell the sender can be supplied along with
the message as the second argument.
To "reply" to a message, simply send a message to its sender.
class Greeter(Actor): def receive(self, message): if message.startswith("Hello"): # reply to the sender self.sender.tell('Hi there!')
Supervision and fault tolerance
Documentation is available at http://pythonhosted.org/actors/.