0mq: pubsub latency continually growing with messa

2019-06-02 16:39发布

pub.py

import zmq
import random
import sys
import time

port = "5556"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)

topic = 10001
while True:
    msgdata = time.time()
    socket.send("%d %d" % (topic, msgdata))
    print "topic:%d, msg:%.5f" % (topic, msgdata)
    time.sleep(1)

sub.py

import sys
import zmq
import time

port = "5556"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

if len(sys.argv) > 2:
    port1 =  sys.argv[2]
    int(port1)

# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print 'connecting to publisher'
socket.connect ("tcp://localhost:%s" % port)

if len(sys.argv) > 2:
    socket.connect ("tcp://localhost:%s" % port1)
topicfilter = "10001"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)

total_value = 0
while True:
    string = socket.recv()
    got_time = time.time()
    topic, msgdata = string.split()
    dur = got_time - float(msgdata)
    print 'it took %.5f from pub' % dur

OUTPUT

connecting to publisher
it took 0.11123 from pub
it took 0.11221 from pub
it took 0.11322 from pub
it took 0.11421 from pub
it took 0.11524 from pub
it took 0.11622 from pub
it took 0.11729 from pub
it took 0.11830 from pub
it took 0.11921 from pub
it took 0.12022 from pub
it took 0.12120 from pub
it took 0.12223 from pub
it took 0.12322 from pub
it took 0.12428 from pub
it took 0.12521 from pub
it took 0.12630 from pub
it took 0.12722 from pub
it took 0.12822 from pub
it took 0.12922 from pub
it took 0.13022 from pub
it took 0.13123 from pub
it took 0.13223 from pub
it took 0.13324 from pub
it took 0.13422 from pub
it took 0.13530 from pub
it took 0.13622 from pub
it took 0.13722 from pub
it took 0.13821 from pub
it took 0.13934 from pub
it took 0.14022 from pub
it took 0.14122 from pub
it took 0.14224 from pub
it took 0.14321 from pub
it took 0.14428 from pub
it took 0.14522 from pub
it took 0.14624 from pub
it took 0.14731 from pub
it took 0.14823 from pub
it took 0.14922 from pub
it took 0.15028 from pub
it took 0.15127 from pub
it took 0.15220 from pub
it took 0.15321 from pub
it took 0.15421 from pub
it took 0.15532 from pub
it took 0.15632 from pub
it took 0.15723 from pub
it took 0.15823 from pub
  1. Why is latency keep growing?
  2. I thought ZMQ has lower latency according to http://zeromq.org/results:more-precise-0mq-tests?
  3. What can i do to lower the latency?

1条回答
2楼-- · 2019-06-02 16:53

The problem is in the computation.
pub.py send the timestamp as an integer :

socket.send("%d %d" % (topic, msgdata))

Replace it with :

socket.send("%d %.5f" % (topic, msgdata))

With this modification it give a nearly constant delay of 0.00030.

查看更多
登录 后发表回答