Package: comotore
Comotore- component engine library - simple actor model realisation.ExamplesClient/server exampleimportasyncioimportloggingimportsysfromcomotoreimport(Engine,Component)fromcomotore.logging.formattersimportPlainFormatterfromcomotoreimportrunnerclassClient(Component):def__init__(self,**kwargs):super().__init__(**kwargs)self._name=kwargs.get("name",None)asyncdefconstruct(self):asyncdefprocess():req=awaitself.request("{name}".format(name=self._name))asyncforresinreq.response():self.logger["Client"].info("Response{name}".format(name=self._name),corr_id=req.correlation_id,payload=res)self.logger["Client"].info("Responses completed{name}".format(name=self._name))awaitself.completed()self.fly(process())@Component.signaldefrequest(self,data):pass@Component.signaldefcompleted(self,_):passclassServer(Component):def__init__(self,**kwargs):super().__init__(**kwargs)self._name=kwargs.get("name",None)asyncdefresponse(self,request):self.logger["Server"].info("Request",payload=request.payload)foriinrange(2):yield"{name}{payload}{number}".format(name=self._name,payload=request.payload,number=i)awaitasyncio.sleep(1)asyncdefmain():engine=Engine()engine.logger.setLevel(logging.INFO)handler=logging.StreamHandler(sys.stderr)formatter=PlainFormatter("{asctime}-{levelname}: [{tag}] hostname={hostname}, pid={process}, tid={thread}>{message}{data}",style="{")handler.setFormatter(formatter)engine.logger.addHandler(handler)server_first=engine.component(Server,name="first")server_second=engine.component(Server,name="second")client=engine.component(Client,name="client_1",start_waiting_for=[server_first.started,server_second.started])engine.call(client.request,server_first.response)engine.call(client.request,server_second.response)engine.cast(client.completed,engine.avatar.quit)print(engine)print("-"*40)awaitengineif__name__=="__main__":runner.run(main())Producer/consumer exampleimportasyncioimportloggingimportuuidimportsysfromcomotoreimport(Engine,Component)fromcomotore.logging.formattersimportPlainFormatterfromcomotoreimportrunnerclassProducer(Component):asyncdefconstruct(self):asyncdefproduce():try:i=0whileTrue:payload=uuid.uuid4()awaitself.produced(payload)self.logger["Producer"].info("Produce",payload=payload)awaitasyncio.sleep(2)i+=1ifi>2:breakawaitself.completed()exceptasyncio.CancelledError:passself.fly(produce())@Component.signaldefproduced(self,uniq_id):pass@Component.signaldefcompleted(self,_):passclassConsumer(Component):asyncdefconsume1(self,signal):self.logger["Consumer"].info("Consume 1",payload=signal.payload)asyncdefconsume2(self,signal):self.logger["Consumer"].info("Consume 2",payload=signal.payload)asyncdefmain():engine=Engine()engine.logger.setLevel(logging.INFO)handler=logging.StreamHandler(sys.stderr)formatter=PlainFormatter("{asctime}-{levelname}: [{tag}] hostname={hostname}, pid={process}, tid={thread}>{message}{data}",style="{")handler.setFormatter(formatter)engine.logger.addHandler(handler)consumer=engine.component(Consumer)producer=engine.component(Producer,start_waiting_for=[consumer.started])engine.cast(producer.produced,consumer.consume1)engine.cast(producer.produced,consumer.consume2)engine.cast(producer.completed,engine.avatar.quit)print(engine)print("-"*40)awaitengineif__name__=="__main__":runner.run(main())Invoke exampleimportasyncioimportloggingimportsysfromcomotoreimport(Engine,Component)fromcomotore.logging.formattersimportPlainFormatterfromcomotoreimportrunnerclassWorker(Component):@Component.signaldefcompleted(self):passasyncdefcast_work(self,signal):self.logger["Worker"].info("Cast work was invoked with",payload=signal.payload)awaitself.completed()asyncdefcall_work(self,signal):self.logger["Worker"].info("Call work was invoked with",payload=signal.payload)foriinrange(2):yield"Call work response{number}".format(number=i)awaitasyncio.sleep(1)asyncdefinvoker(eng,cast_target,call_target):eng.logger["Invoker"].info("Invoke in 2 seconds")awaitasyncio.sleep(2)eng.logger["Invoker"].info("Invoke call")req=awaiteng.invoke_call(call_target,payload="Some call data")asyncforresinreq.response():eng.logger["Invoker"].info(res)awaiteng.invoke_cast(cast_target,payload="Some cast data")asyncdefmain():engine=Engine()engine.logger.setLevel(logging.INFO)handler=logging.StreamHandler(sys.stderr)formatter=PlainFormatter("{asctime}-{levelname}: [{tag}] hostname={hostname}, pid={process}, tid={thread}>{message}{data}",style="{")handler.setFormatter(formatter)engine.logger.addHandler(handler)worker=engine.component(Worker)engine.cast(worker.completed,engine.avatar.quit)print(engine)print("-"*40)asyncio.ensure_future(invoker(engine,worker.cast_work,worker.call_work),loop=engine.loop)awaitengineif__name__=="__main__":runner.run(main())