ZeroMQ:多个套接字的相同上下文
ZeroMQ: Same context for multiple sockets
我正在尝试使用 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 上下文用法的文档部分以及我在本文末尾为您制作的示例 post。
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 context
和 ZMQ PUB-SUB
模式。只要您有 5 个生产服务,就可以创建 5 个订户套接字。但是,如果您有一个来源发布通知,我建议使用 PUB-SUB
模式并过滤 属性 of ZMQ SUB
套接字。您可以在下面我的代码中查看如何在 publisher #1
和 subscriber
之间进行通信来设置它。
发布者 #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 的发布-订阅套接字。但是,我在创建套接字(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 上下文用法的文档部分以及我在本文末尾为您制作的示例 post。
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 forinproc
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 context
和 ZMQ PUB-SUB
模式。只要您有 5 个生产服务,就可以创建 5 个订户套接字。但是,如果您有一个来源发布通知,我建议使用 PUB-SUB
模式并过滤 属性 of ZMQ SUB
套接字。您可以在下面我的代码中查看如何在 publisher #1
和 subscriber
之间进行通信来设置它。
发布者 #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 )