退房一个跨平台的库/服务器调用的RabbitMQ。对于双进程通信来说可能过于沉重,但如果您需要多进程或多代码库通信(使用各种不同的方式,例如一对多,队列等),这是一个不错的选择。
要求:
$ pip install pika
$ pip install bson # for sending binary content
$ sudo apt-get rabbitmq-server # ubuntu, see rabbitmq installation instructions for other platforms
出版商(发送数据):
import pika, time, bson, os
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', type='fanout')
i = 0
while True:
data = {'msg': 'Hello %s' % i, b'data': os.urandom(2), 'some': bytes(bytearray(b'\x00\x0F\x98\x24'))}
channel.basic_publish(exchange='logs', routing_key='', body=bson.dumps(data))
print("Sent", data)
i = i + 1
time.sleep(1)
connection.close()
订户(接收数据,可以是多个):基于https://www.rabbitmq.com/tutorials/tutorial-two-python.html
感谢,非常好 – wim
可以修改这个工作在多线程环境吗?我现在有几个工作线程连接,并且它似乎导致zmq c代码断言失败 – wim
您也可以用pizco打包zmq: https://pizco.readthedocs.io/en/latest/ –