Package: amqp-client-python
AMQP Client PythonClient with high level of abstraction for manipulation of messages in the event bus RabbitMQ.Features:Automatic creation and management of queues, exchanges and channels;Connection persistence and auto reconnect;Support fordirect,topicandfanoutexchanges;Publish;Subscribe;Support for a Remote procedure call(RPC).Examples:you can usesync,async eventbusandsync wrapperof async eventbusasync usage# basic configurationfromamqp_client_pythonimport(AsyncEventbusRabbitMQ,Config,Options)fromamqp_client_python.eventimportIntegrationEvent,IntegrationEventHandlerconfig=Config(Options("queue","rpc_queue","rpc_exchange"))eventbus=AsyncEventbusRabbitMQ(config)# publishclassExampleEvent(IntegrationEvent):EVENT_NAME:str="ExampleEvent"def__init__(self,event_type:str,message=[])->None:super().__init__(self.EVENT_NAME,event_type)self.message=messagepublish_event=ExampleEvent(rpc_exchange,["message"])eventbus.publish(publish_event,rpc_routing_key,"direct")# subscribeclassExampleEventHandler(IntegrationEventHandler):asyncdefhandle(self,body)->None:print(body)# handle messagesawaiteventbus.subscribe(subscribe_event,subscribe_event_handle,rpc_routing_key)# rpc_publishresponse=awaiteventbus.rpc_client(rpc_exchange,"user.find",["content_message"])# providerasyncdefhandle2(*body)->bytes:print(f"body:{body}")returnb"content"awaiteventbus.provide_resource("user.find",handle)sync usagefromamqp_client_pythonimport(EventbusRabbitMQ,Config,Options)fromamqp_client_python.eventimportIntegrationEvent,IntegrationEventHandlerfromexamples.defaultimportqueue,rpc_queue,rpc_exchange,rpc_routing_keyclassExampleEvent(IntegrationEvent):EVENT_NAME:str="ExampleEvent"ROUTING_KEY:str=rpc_routing_keydef__init__(self,event_type:str,message=[])->None:super().__init__(self.EVENT_NAME,event_type)self.message=messageself.routing_key=self.ROUTING_KEYclassExampleEventHandler(IntegrationEventHandler):defhandle(self,body)->None:print(body,"subscribe")config=Config(Options(queue,rpc_queue,rpc_exchange))eventbus=EventbusRabbitMQ(config=config)classExampleEvent(IntegrationEvent):EVENT_NAME:str="ExampleEvent"def__init__(self,event_type:str,message=[])->None:super().__init__(self.EVENT_NAME,event_type)self.message=messagefromtimeimportsleepfromrandomimportrandintdefhandle(*body):print(body[0],"rpc_provider")returnf"{body[0]}".encode("utf-8")subscribe_event=ExampleEvent(rpc_exchange)publish_event=ExampleEvent(rpc_exchange,["message"])subscribe_event_handle=ExampleEventHandler()eventbus.subscribe(subscribe_event,subscribe_event_handle,rpc_routing_key)eventbus.provide_resource(rpc_routing_key+"2",handle)count=0running=Truefromconcurrent.futuresimportTimeoutErrorwhilerunning:try:count+=1ifstr(count)!=eventbus.rpc_client(rpc_exchange,rpc_routing_key+"2",[f"{count}"]).decode("utf-8"):running=False#eventbus.publish(publish_event, rpc_routing_key, "direct")#running = FalseexceptTimeoutErroraserr:print("timeout!!!: ",str(err))exceptKeyboardInterrupt:running=FalseexceptBaseExceptionaserr:print("Err:",err)sync wrapper usagefromamqp_client_pythonimportEventbusWrapperRabbitMQ,Config,Optionsfromamqp_client_python.eventimportIntegrationEvent,IntegrationEventHandlerconfig=Config(Options(queue,rpc_queue,rpc_exchange))eventbus=EventbusWrapperRabbitMQ(config=config)classExampleEvent(IntegrationEvent):EVENT_NAME:str="ExampleEvent"def__init__(self,event_type:str,message=[])->None:super().__init__(self.EVENT_NAME,event_type)self.message=messageclassExampleEventHandler(IntegrationEventHandler):asyncdefhandle(self,body)->None:print(body,"subscribe")asyncdefhandle(*body):print(body[0],"rpc_provider")returnf"{body[0]}".encode("utf-8")subscribe_event=ExampleEvent(rpc_exchange)publish_event=ExampleEvent(rpc_exchange,["message"])subscribe_event_handle=ExampleEventHandler()# rpc_providereventbus.provide_resource(rpc_routing_key+"2",handle).result()# subscribeeventbus.subscribe(subscribe_event,subscribe_event_handle,rpc_routing_key).result()count=0running=Truewhilerunning:try:count+=1# rpc_client calleventbus.rpc_client(rpc_exchange,rpc_routing_key+"2",[f"{count}"]).result().decode("utf-8")# publisheventbus.publish(publish_event,rpc_routing_key,"direct").result()#running = FalseexceptKeyboardInterrupt:running=FalseexceptBaseExceptionaserr:print("Err:",err)Know Limitations:basic eventbusWhen usingEventbusRabbitMQShould not use rpc call inside of rpc provider and subscribe handlers, it may block the ioloop
#/obs: fixed on other kinds of eventbus, will be removed on nexts releases