问题
I am fairly new to pyzmq
. I am trying to understand inproc:
transport class and have created this sample example to play with.
It looks a Publisher
instance is publishing messages but Subscriber
instances are not receiving any.
In case I move Subscriber
instances into a separate process
and change inproc:
to a tcp:
transport class, the example works.
Here is the code:
import threading
import time
import zmq
context = zmq.Context.instance()
address = 'inproc://test'
class Publisher(threading.Thread):
def __init__(self):
self.socket = context.socket(zmq.PUB)
self.socket.bind(address)
def run(self):
while True:
message = 'snapshot,current_time_%s' % str(time.time())
print 'sending message %s' % message
self.socket.send(message)
time.sleep(1)
class Subscriber(object):
def __init__(self, sub_name):
self.name = sub_name
self.socket = context.socket(zmq.SUB)
self.socket.connect(address)
def listen(self):
while True:
try:
msg = self.socket.recv()
a, b = msg.split(' ', 1)
print 'Received message -> %s-%s-%s' % (self.name, a, b)
except zmq.ZMQError as e:
logger.exception(e)
if __name__ == '__main__':
thread_a = []
for i in range(0, 1):
subs = Subscriber('subscriber_%s' % str(i))
th = threading.Thread(target=subs.listen)
thread_a.append(th)
th.start()
pub = Publisher()
pub_th = threading.Thread(target=pub.run)
pub_th.start()
回答1:
There is nothing wrong, but
ZeroMQ is a wonderfull toolbox.
It is full of smart, bright and self-adapting services under the hood, that literally save our poor lives in many ways.
Still it is worth to read and obey a few rules from the documentation.
inproc
transport class has one such. .bind()
first, before .connect()
-s
[ Page 38, Code Connected, Volume I ]
...inproc
is an inter-thread signalling transport ... it is faster thantcp
oripc
. This transport has a specific limitation compared totpc
andicp
: the server must issue abind
before any client issues aconnect
. This is something future versions of ØMQ may fix, but at present this defines how you useinproc
sockets.
So, as an example:
if __name__ == '__main__':
pub = Publisher()
pub_th = threading.Thread( target = pub.run )
pub_th.start()
# give it a place to start before .connect()-s may take place
# give it a time to start before .connect()-s may take place
sleep(0.5)
thread_a = []
for i in range( 0, 1 ):
subs = Subscriber( 'subscriber_%s' % str( i ) )
th = threading.Thread( target = subs.listen )
thread_a.append( th )
th.start()
来源:https://stackoverflow.com/questions/34012294/zeromq-subscribers-not-receiving-message-from-publisher-over-an-inproc-transpor