Package: pyctor
🎭 pyctorMinimalistic implementation of the actor concurrency model powered by asyncio.Installation:pip install pyctor(requires Python>= 3.7.3)APIOverviewPyctor 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 theActorclass:classMyActor(pyctor.Actor):asyncdefreceive(self,sender,message):print(f'received message:{message}')Every actor must override thereceivemethod. 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 calledActorSystem. The actor system controls the lifecycle of every actor and handles message passing between actors. Actors may override lifecycle methods (e.g.,started,restartedorstopped) 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:asyncdefmain():system=pyctor.ActorSystem()# create actor systemmy_actor=system.create(MyActor())# create first actorsystem.tell(my_actor,'hello')# send message to the actorawaitsystem.stopped()# await system shutdownThecreatemethod 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 thetellmethod 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 theasyncio.runmethod:if__name__=='__main__':asyncio.run(main())APIpyctor.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.