I'm using Python bindings for ZeroMQ. My libzmq
version is 4.2.5 and my pyzmq
version is 17.1.2.
I'm trying to let a "producer" transmit a large amount of data to a "consumer". The code of the "producer" is :
# producer.py
import zmq
import time
import os
ctx = zmq.Context()
sock = ctx.socket(zmq.PUB)
sock.bind('tcp://*:8000')
x = os.urandom(1000000000) # large amount of data, requires much time to transmit it
sock.send(x)
print('Done')
sock.setsockopt(zmq.LINGER, 0)
sock.close()
t1 = time.time()
ctx.term() # I expect this should return immediately
print(time.time() - t1)
And the code of "consumer" is :
# consumer.py
import zmq
ctx = zmq.Context()
sock = ctx.socket(zmq.SUB)
sock.setsockopt_string(zmq.SUBSCRIBE, '')
sock.connect('tcp://localhost:8000')
data = sock.recv()
I expect the ctx.term()
in the producer.py
should return immediately, since the LINGER
of the socket is already set to zero. But when I run these codes, the ctx.term()
does not return immediately as expected. Instead, it takes tens of seconds for that function to return, and all of the large data have been successfully received by the consumer.py
.
I am trying to figure out why, and I wish someone help me out a little.