pyzmq服务器/客户端基于min-rk启动代码实现异步自由协议
zmqflp的Python项目详细描述
ZMQFLP
对基于自由职业者协议的ZeroMQ服务器/客户端(Python)的改进 客户机和服务器使用cbor2进行对话,因此api接受字典作为输入。
创建zmqflp服务器:
# create the server object (it runs in an asyncio zmq context)self.server=zmqflp_server.ZMQFLPServer(self.config.identity,self.config.zmq_port)# use the following code to process messages received by the server and send them backasyncdefprocess_messages(self):(serialized_request,orig_headers)=awaitself.server.receive()ifserialized_request=='EXIT':awaitself.server.send(orig_headers,'exiting')returnFalseelifserialized_request!="PING":try:request=serialized_requestresponse=self.process_request(request)awaitself.server.send(orig_headers,response)returnTrueexceptExceptionase:logging.exception(e)returnFalsereturnTrue
不使用上下文管理器创建客户端:
# create the client object (this does NOT run in an asyncio context)self.client=zmqflp_client.ZMQFLPClient(self.config.list_of_servers)# to send and receive with the clientmsg_to_send={'message':'hello!','other-key':'stuff goes here'}status=self.client.send_and_receive(msg_to_send)
使用上下文管理器创建客户机(例如,在aws lambda上运行):
# create the client object (this does NOT run in an asyncio context)withzmqflp_client.ZMQFLPClient(self.config.list_of_servers)asclient:# to send and receive with the clientmsg_to_send={'message':'hello!','other-key':'stuff goes here'}status=client.send_and_receive(msg_to_send)