1: port -6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:18:57 30 4
gpt4 key购买 nike

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)

子.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

输出

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. 为什么延迟时间不断增加?
  2. 根据http://zeromq.org/results:more-precise-0mq-tests我认为ZMQ具有较低的延迟?
  3. 我可以采取什么措施来降低延迟?

最佳答案

问题出在计算上。
pub.py 以整数形式发送时间戳:

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

将其替换为:

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

通过此修改,它给出了几乎恒定的 0.00030 延迟。

关于python - 0mq : pubsub latency continually growing with messages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26412694/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com