ZeroMQ:多个套接字的相同上下文

标签 zeromq pyzmq jzmq

我正在尝试使用 ZeroMQ 的发布-订阅套接字。但是,我不清楚创建套接字(zmq::socket_t)时上下文(zmq::context_t)的作用。

假设我想创建 5 个订阅者套接字(zmq::socket_t 使用 ZMQ_SUB),我是否需要 5 个上下文,每个订阅者套接字一个?或者我可以对所有 5 个套接字使用单个上下文吗?

最佳答案

Assuming that I want to create 5 subscriber sockets ( zmq::socket_t using ZMQ_SUB ), do I need 5 contexts, one for each of the subscriber sockets? Or can I use a single context for all 5 sockets?

对于这一轻量级用例,您只需要一个 Context 实例。检查下面附加的解释 0MQ 上下文用法的文档部分以及我在本文末尾附加的为您制作的示例。

ZeroMQ applications always start by creating a context, and then using that for creating sockets. In C, it's the zmq_ctx_new() call. You should create and use exactly one context in your process. Technically, the context is the container for all sockets in a single process, and acts as the transport for inproc sockets, which are the fastest way to connect threads in one process. If at runtime a process has two contexts, these are like separate ZeroMQ instances.

我在下面为您做了一个示例,以帮助您理解 ZMQ contextZMQ PUB-SUB 模式。只要您有 5 个生产服务,创建 5 个订阅者套接字就可以了。但是,如果您有一个源发布通知,我建议使用 PUB-SUB 模式和 ZMQ SUB 套接字的过滤属性。您可以在下面的代码中检查如何在 publisher #1subscriber 之间进行通信。

发布商 #1 发送温度和湿度更新..

import zmq
from time import sleep

# Server socket
context = zmq.Context()
socket  = context.socket( zmq.PUB )
socket.bind( "tcp://*:5556" )

while True:
    socket.send_multipart( [ "TEMP", "25.40" ] )
    socket.send_multipart( [ "HUMD", "48.90" ] )
    sleep( 1 )

发布商 #2 发送压力更新..

import zmq
from time import sleep

# Server socket
context = zmq.Context()
socket2 = context.socket( zmq.PUB )
socket2.bind( "tcp://*:5557" )

while True:
    socket2.send_multipart( [ "PRSS", "10000.00" ] )
    sleep( 1 )

订阅者在两个不同的服务器上注册温度、湿度和压力更新。

import zmq
from time import sleep

# Sockets to talk to servers
context = zmq.Context()
socket  = context.socket( zmq.SUB )
socket.connect(  "tcp://localhost:5556" )
socket2 = context.socket( zmq.SUB )
socket2.connect( "tcp://localhost:5557" )

# Set filters
socket.setsockopt_string(  zmq.SUBSCRIBE, "TEMP".decode( 'ascii' ) )
socket.setsockopt_string(  zmq.SUBSCRIBE, "HUMD".decode( 'ascii' ) )
socket2.setsockopt_string( zmq.SUBSCRIBE, "PRSS".decode( 'ascii' ) )

poller = zmq.Poller()
poller.register( socket,  zmq.POLLIN )
poller.register( socket2, zmq.POLLIN )

while True:
    socks = dict( poller.poll() )
    if socket in socks and socks[socket] == zmq.POLLIN:
        [ measurement, value ] = socket.recv_multipart()
        print measurement
        print value

    if socket2 in socks and socks[socket2] == zmq.POLLIN:
        [ measurement, value ] = socket2.recv_multipart()
        print measurement
        print value

    sleep( 1 )

关于ZeroMQ:多个套接字的相同上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32280271/

相关文章:

java - 如何使用 ZeroMQ 将字节数组发送到套接字?

python - 0mq : pubsub latency continually growing with messages?

ubuntu - ZeroMQ Java 示例在 Ubuntu 12 上挂起

java - ZMQ 是否压缩消息?

java - zmq jar 的版本

python - 如何在 ZeroMQ 中以正确的方式中止 context.socket.recv()?

python - ZeroMQ PUB 套接字在连接时缓冲我所有的外出数据

c++ - 将 zmq::proxy 与 REQ/REP 模式结合使用

zeromq - 使用 ZMQ 从多个客户端拉取请求

python - zmq.error.ZMQError : Address already in use, 使用 papermill 对多个笔记本运行多处理时