Minimalistic implementation of the actor concurrency model powered by asyncio.
Project description
🎭 pyctor
Minimalistic implementation of the actor concurrency model powered by asyncio.
- Installation:
pip install pyctor(requires Python>= 3.7.3)
- API
Overview
Pyctor is a lightweight implementation of the actor model for concurrent systems. Actors should only modify their own private state. They can influence other actors in the system through messaging. In Pyctor, we define actors by subclassing the Actor class:
class MyActor(pyctor.Actor):
async def receive(self, sender, message):
print(f'received message: {message}')
Every actor must override the receive method. Inside it, the actor responds to the received messages, e.g., by modifying the internal state, sending new messages or creating new actors.
All actors live in a single ecosystem called ActorSystem. The actor system controls the lifecycle of every actor and handles message passing between actors. Actors may override lifecycle methods (e.g., started, restarted or stopped) that will be called by the system at specific points throughout actor's life. In the main function below, which serves as an entry point, we create the actor system and the first actor:
async def main():
system = pyctor.ActorSystem() # create actor system
my_actor = system.create(MyActor()) # create first actor
system.tell(my_actor, 'hello') # send message to the actor
await system.stopped() # await system shutdown
The create method starts an actor and returns a reference for that actor. Actors may create child actors, forming a hierarchy of actors in the system. Actors should not be modified directly. Instead, they should communicate through messages. Messages are sent with the tell method which expects a reference of the actor that shall receive the message. The actor system guarantees that the messages are received in the same order they were sent in.
We run the actor system by passing the main function to the asyncio.run method:
if __name__ == '__main__':
asyncio.run(main())
API
-
pyctor.ActorSystemcreate(actor, name=None)
Start an actor.tell(actor, message)
Deliver a message to an actor.schedule_tell(actor, message, *, delay=None, period=None)
Schedule a message to be delivered to an actor at some time. The message may also be delivered periodically.stop(actor)
Stop an actor.shutdown(timeout=None)
Initialize a shutdown of the actor system.stopped()
Await system shutdown.
-
pyctor.Actorreceive(sender, message)
This method is called by the system every time a message is received by the actor. It must be implemented by the class inheriting from Actor.create(actor, name=None)
Start a child actor. The child actor will be watched.tell(actor, message)
Deliver a message to another actor.schedule_tell(actor, message, *, delay=None, period=None)
Schedule a message to be delivered to another actor at some time. The message may also be delivered periodically.watch(actor)
Watch another actor and receive aTerminated(actor)message when the watched actor stops.unwatch(actor)
Stop watching another actor.stop()
Stop the actor.
Lifecycle callbacks:
started()
Called by the system before the actor starts to receive messages.restarted(sender, message, error)
Called by the system if receiving the message caused an error. Actor will continue to receive messages.stopped()
Called by the system before stopping the actor. The actor will not receive messages anymore.
-
System messages:
PoisonPill()
Stop an actor.Terminated(actor)
Notify that the actor has stopped.DeadLetter(actor, message)
Notify the sender that the actor did not receive the message.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyctor-0.0.2.tar.gz.
File metadata
- Download URL: pyctor-0.0.2.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49555e4e9e5f1af230d80bc7d7bb923098473e092cc89f4889c53831667f7c6e
|
|
| MD5 |
4fab24ada581cdf000518fb7b7dec6cb
|
|
| BLAKE2b-256 |
abb7035973b4091f43a84e18610b5275835298774ab69a9edc4fd6e926c76b1b
|
File details
Details for the file pyctor-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pyctor-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d178952416c03551f0433ffddda0b4c916bfabecc5bbd361669978373bacb7a
|
|
| MD5 |
1584dce68fa4eebcbf145e4485dbeaf9
|
|
| BLAKE2b-256 |
523b6c895dbefc322f75fb80c975b115abe57e1a96d576a4d9c72dacd7b164b5
|