def main(): max_wait = timedelta(seconds=10) #system = ActorSystem('multiprocTCPBase') system = ActorSystem() parent = system.createActor(ParentProcessor) system.tell(parent, 1) print(system.listen(max_wait))
class Supervisor: """ Interface with Actor system Used to launch, stop and monitor actors """ def __init__(self, verbose_mode: bool): """ :param verbose_mode: Specify the log level : True -> DEBUG False -> INFO """ if verbose_mode: LOG_DEF['handlers']['h1']['level'] = logging.DEBUG LOG_DEF['handlers']['h2']['level'] = logging.DEBUG else: LOG_DEF['handlers']['h1']['level'] = logging.INFO LOG_DEF['handlers']['h2']['level'] = logging.INFO self.system = ActorSystem(systemBase='multiprocQueueBase', logDefs=LOG_DEF) self.pushers = {} self.pullers = {} self.dispatchers = {} self.actors = {} def launch(self, actor_cls: Type[Actor], start_message: StartMessage): """ create an actor from a given class and send it a start message. :param actor_cls: class used to create the actor :param start_message: message used to initialize the actor :raise InitializationException: if an error occurs during actor initialiszation process """ name = start_message.name address = self.system.createActor(actor_cls) answer = self.system.ask(address, start_message) if isinstance(answer, OKMessage): self._add_actor(address, name, actor_cls) logging.info('launch %s actor', name) return address elif isinstance(answer, ErrorMessage): raise InitializationException(answer.error_message) raise InitializationException("Unknow message type : " + str(type(answer))) def _add_actor(self, address, name, actor_cls): if issubclass(actor_cls, PusherActor): self.pushers[name] = address elif issubclass(actor_cls, PullerActor): self.pullers[name] = address elif issubclass(actor_cls, DispatcherActor): self.dispatchers[name] = address else: raise AttributeError( 'Actor is not a DispatcherActor, PusherActor or PullerActor') self.actors[name] = address def _wait_actors(self): for _ in self.pushers: self.system.listen() def shutdown(self): """ Shutdown the entire actor system and all the actors """ self.system.shutdown() def monitor(self): """ wait for an actor to send an EndMessage or for an actor to crash """ while True: msg = self.system.listen(1) if msg is None: pass elif isinstance(msg, EndMessage): self._wait_actors() return else: logging.error("Unknow message type : %s", str(type(msg)))
import sys from datetime import timedelta from thespian.actors import ActorSystem if __name__ == "__main__": # We take the convention leaders address from the command line # Also, we tag this system with "Client" capabilities = { "Convention Address.IPv4": (sys.argv[1], 1900), "Client": True } actor_system = ActorSystem("multiprocTCPBase", capabilities) # We create an actor from the echo library with class EchoRequestor echo_app = actor_system.createActor("echo.EchoRequestor") # Send the echo actor a message: the number of echo requests it should perform actor_system.tell(echo_app, int(sys.argv[2])) # Now, send the echo payload, and wait max. 10s for an answer resp = actor_system.ask(echo_app, "hello world", timedelta(seconds=10)) while resp: # If we get "echo_done" as an answer we break out if resp == "echo_done": break # Otherwise we'll retry to get a response print("unexpected message {}".format(resp)) resp = actor_system.listen(timedelta(seconds=10))