Pulsar implements two layers of components on top of python
- The actor layer provides parallel execution in
processes and threads and uses the
multiprocessingmodules as building blocks.
- The application framework is built on top of
the actor layer and it is based on the higher level
Applicationclass which provides an elegant API for speeding up development of asynchronous and parallel software.
Actor is the atom of pulsar’s concurrent computation,
they do not share state between them, communication is achieved via asynchronous
inter-process message passing,
asyncio socket utilities.
Messages are exchanged using single bidirectional connections between any actor and are encoded using the unmasked websocket protocol as the actor messages tutorial highlights. A pulsar actor can be process based as well as thread based and can perform one or many activities.
The actor model is the cornerstone of the Erlang programming language. Python has very few implementation and all of them seem quite limited in scope.
The Actor model in computer science is a mathematical model of concurrent computation that treats “actors” as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received.
- Each actor has its own
process(not intended as an OS process) and they don’t shares state between them.
- Actors can change their own states.
- Actors can create other actors and when they do that they receive back the new actor address.
- Actors exchange messages in an asynchronous fashion.
Why would one want to use an actor-based system?
- No shared memory and therefore locking is not required.
- Race conditions greatly reduced.
- It greatly simplifies the control flow of a program, each actor has its own process (flow of control).
- Easy to distribute, across cores, across program boundaries, across machines.
- It simplifies error handling code.
- It makes it easier to build fault-tolerant systems.
An actor can be processed based (default) or thread based and controls one running event loop. To obtain the actor controlling the current thread:
actor = pulsar.get_actor()
When a new processed-based actor is created, a new process is started and the actor takes control of the main thread of that new process. On the other hand, thread-based actors always exist in the master process (the same process as the arbiter) and control threads other than the main thread.
Regardless of the type of concurrency, an actor always controls at least one thread, the actor io thread. In the case of process-based actors this thread is the main thread of the actor process.
An actor is a async object and therefore it has
attribute, which can be used to register handlers on file descriptors.
Actor._loop is created just after forking (or after the
actor’s thread starts for thread-based actors).
The most common usage for an
Actor is to handle Input/Output
events on file descriptors. An
the operating system (through
select) that it should be notified
when a new connection is made, and then it goes to sleep.
Serving the new request should occur as fast as possible so that other
connections can be served simultaneously.
Another way for an actor to function is to use its
to perform CPU intensive operations, such as calculations, data manipulation
or whatever you need them to do.
Periodic task are implemented by the
When using pulsar actor layer, you need to use pulsar in server state,
that is to say, there will be a centralised Arbiter controlling the main
event loop in the main thread of the
The arbiter is a specialised
which control the life of all
To access the arbiter, from the main process, one can use the
arbiter() high level function:
>>> arbiter = pulsar.arbiter() >>> arbiter.is_running() False
To aid the development of applications running on top of pulsar concurrent
framework, the library ships with the
Applications can be of any sorts or forms and the library is shipped
with several battery included examples in the pulsar.apps module.
Monitors are specialised actors which share the arbiter event loop and therefore live in the main thread of the master process of your application.
It is possible to configure pulsar so that the arbiter delegates the management of some actors to monitors. The application layer is designed specifically to obtain such delegation in a straightforward way with an efficient and elegant API.
Spawning a new actor is achieved via the
from pulsar import spawn def task(actor, exc=None): # do something useful here ... ap = spawn(periodic_task=task)
The handshake occurs when the monitor receives, for the first time, the actor notify message.
For the curious, the handshake is responsible for setting the
If the hand-shake fails, the spawned actor will eventually stop.
Actor exposes three one time events
which can be used to customise its behaviour and two
many times event used when accessing actor
information and when the actor spawn other actors.
Hooks are passed as key-valued parameters to the
Fired just after the actor has received the
hand-shake from its monitor. This hook can be used to setup
the application and register event handlers. For example, the
socket server application creates the server and register
its file descriptor with the
This snippet spawns a new actor which starts an Echo server:
from functools import partial from pulsar import spawn, TcpServer def create_echo_server(address, actor, _): '''Starts an echo server on a newly spawn actor''' server = TcpServer(actor.event_loop, address, address, EchoServerProtocol) yield server.start_serving() actor.servers['echo'] = server actor.extra['echo-address'] = server.address proxy = spawn(start=partial(create_echo_server, 'localhost:9898'))
Fired when the
Actor starts stopping.
Fired at every actor periodic task (More docs here)
Fired every time the actor status information is accessed via the info command:
def extra_info(actor, info=None): info['message'] = 'Hello' proxy = spawn(on_info=extra_info)
The hook must accept the actor as first parameter and the
info (a dictionary).
Fired every time an actor is about to spawn another actor. It can be used to
add additional key-valued parameters passed to the
Actor communicates with another remote
Actor by sending
an action to perform. This action takes the form of a command name and
optional positional and key-valued parameters. It is possible to add new
commands via the
command decorator as explained in the
Ping the remote actor
abcd and receive an asynchronous
received an asynchronous echo from a remote actor
send('abcd', 'echo', 'Hello!')
Request information about a remote actor
The asynchronous result will be called back with the dictionary returned
This message is used periodically by actors, to notify their manager. If an
actor fails to notify itself on a regular basis, its manager will shut it down.
notify message is sent to the manager as soon as the actor is up
and running so that the handshake can occur.
Run a function on a remote actor. The function must accept actor as its initial parameter:
def dosomething(actor, *args, **kwargs): ... send('monitor', 'run', dosomething, *args, **kwargs)
Tell the remote actor
abc to gracefully shutdown:
There are two categories of exceptions in Python: those that derive from the
Exception class and those that derive from
Exceptions deriving from Exception will generally be caught and handled
appropriately; for example, they will be passed through by a
and they will be logged and ignored when they occur in a callback.
However, exceptions deriving only from BaseException are never caught, and will usually cause the program to terminate with a traceback. (Examples of this category include KeyboardInterrupt and SystemExit; it is usually unwise to treat these the same as most other exceptions.)
For example this is a class for valid async objects:
from pulsar import get_event_loop, new_event_loop class SimpleAsyncObject: def __init__(self, loop=None): self._loop = loop or get_event_loop() or new_event_loop()
- Several classes in pulsar are async objects, for example:
Storeand so forth
Futureis an async object
- However an async object is not necessarily a
- When they use and
task()decorators for their methods,
_loopattribute is used to run the method
- Pulsar provides the
AsyncObjectsignature class, however it is not a requirement to derive from it
An async object can also run its asynchronous methods in a synchronous fashion. To do that, one should pass a bright new event loop during initialisation. Check synchronous components for further details.