async def setup(self) -> None: props = Props.from_producer(lambda: PidCacheWatcher()) \ .with_guardian_supervisor_strategy(Supervision.always_restart_strategy) self._watcher = GlobalRootContext.spawn_named(props, 'PidCacheWatcher') self._cluster_topology_evn_sub = GlobalEventStream.subscribe( self.process_member_status_event, type(AbstractMemberStatusEvent))
async def receive(self, context: AbstractContext) -> None: message = context.message if isinstance(message, ActorPidRequest): props = Remote().get_known_kind(message.kind) name = message.name if name is None: name = ProcessRegistry().next_id() try: pid = GlobalRootContext.spawn_named(props, name) response = ActorPidResponse(pid=pid) await context.respond(response) except ProcessNameExistException as ex: response = ActorPidResponse( pid=ex.pid, status_code=int( ResponseStatusCode.ProcessNameAlreadyExist)) await context.respond(response) except ActivatorException as ex: response = ActorPidResponse(status_code=ex.code) await context.respond(response) if not ex.do_not_throw: raise Exception() except Exception: response = ActorPidResponse( status_code=int(ResponseStatusCode.Error)) await context.respond(response) raise Exception()
async def run_test(mailbox: Callable[..., AbstractMailbox]): props = Props.from_func(process_message) \ .with_mailbox(mailbox) pid = GlobalRootContext.spawn(props) for i in range(10000): await GlobalRootContext.send(pid, i) await GlobalRootContext.request_future(pid, 'stop')
def start(self) -> None: self._logger.debug('Started EndpointManager') props = Props().from_producer(EndpointSupervisor) \ .with_guardian_supervisor_strategy(Supervision.always_restart_strategy) self._endpoint_supervisor = GlobalRootContext.spawn_named( props, 'EndpointSupervisor') self._endpoint_conn_evn_sub = GlobalEventStream.subscribe( self.__on_endpoint_connected, EndpointConnectedEvent) self._endpoint_term_evn_sub = GlobalEventStream.subscribe( self.__on_endpoint_terminated, EndpointTerminatedEvent)
async def receive(self, context: AbstractContext): msg = context.message if isinstance(msg, Request): if msg.size == 1: await context.respond(msg.num) await context.stop(context.my_self) return self._replies = msg.div self._reply_to = context.sender for i in range(msg.div): child = GlobalRootContext.spawn(props) await context.request( child, Request(num=msg.num + i * (msg.size // msg.div), size=msg.size // msg.div, div=msg.div)) elif isinstance(msg, int): self._sum += msg self._replies -= 1 if self._replies == 0: await context.send(self._reply_to, self._sum)
async def spawn_partition_actor(kind: str) -> PID: props = Props.from_producer(lambda: PartitionActor(kind)) \ .with_guardian_supervisor_strategy(Supervision.always_restart_strategy) return GlobalRootContext.spawn_named(props, 'partition-' + kind)
def __spawn_activator(self): props = Props().from_producer(Activator) \ .with_guardian_supervisor_strategy(Supervision.always_restart_strategy) self._activator_pid = GlobalRootContext.spawn_named(props, 'activator')