面向对象的amqp层,用于微服务通信。
amqp-ko的Python项目详细描述
amqp k_
面向对象的amqp层,用于微服务通信。
用法
建议使用amqp k_的方法是创建自己的队列对象。最简单的方法是使用createQueue
函数。
创建队列
fromamqp_koimportcreate_queue,AsyncConnection,Message,MessageGatefromdataclassesimportdataclass@dataclass(frozen=True)classTopicFollow(Message):user_id:inttopic_name:strdefunmarshal_topic_follow(data:dict)->TopicFollow:returnTopicFollow(user_id=data["user_id"],topic_name=data["topic_name"],)message_gates=[MessageGate("topic_follow",TopicFollow,unmarshal_topic_follow),]asyncwithAsyncConnection("localhost",5672,"rabbitmq","rabbitmq")asconnection:queue=awaitcreate_queue(connection,"exchange-name",message_gates)
使用消息
fromamqp_koimportConsumer,JobclassConnectUserWithTopic(Consumer):asyncdefconsume(self,job:Job):# Put here some code to connect user with a topic# using "job.message.userId" and "job.message.topicName"awaitjob.ack()awaitqueue.consume("queue-name",{TopicFollow:ConnectUserWithTopic()},)
生成消息
message=TopicFollow(120,"entertainment")awaitqueue.produce(message)
安装
pip install amqp-ko