【发布时间】:2014-05-02 01:34:27
【问题描述】:
我发现了一个类似的问题,ZeroMQ: HWM on PUSH does not work,但它无法解决我的问题。
我想控制推送套接字排队的消息数量,但它不起作用,仍然排队 1000 条消息。
所以我想知道如何设置推送套接字的hwm。提前致谢。
我的环境是:libzmq 4.0.4、pyzmq 14.1.0、python 3.3
这是我的代码:
server.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import zmq
class TestPush(object):
def __init__(self):
self.ctx = zmq.Context()
random.seed()
def run(self):
task_snd = self.ctx.socket(zmq.PUSH)
task_snd.setsockopt(zmq.SNDHWM, 10)
task_snd.bind('tcp://*:53000')
while True:
workload = str(random.randint(1, 100))
task_snd.send(workload.encode('utf-8'))
print('Send {0}'.format(workload))
if __name__ == '__main__':
test_push = TestPush()
test_push.run()
client.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import random
import zmq
class TestPull(object):
def __init__(self):
self.ctx = zmq.Context()
def run(self):
task_rcv = self.ctx.socket(zmq.PULL)
task_rcv.setsockopt(zmq.RCVHWM, 1)
task_rcv.connect('tcp://localhost:53000')
while True:
msg = task_rcv.recv()
print('Receive msg: {0}'.format(msg))
time.sleep(random.randint(2, 3))
if __name__ == '__main__':
test_pull = TestPull()
test_pull.run()
【问题讨论】:
-
你能在设置后用“getsocktopt”函数读取HWM值吗?因此您可以验证是否设置了值。
-
@AhmetKakıcı 是的,
getsockopt()返回的值表明对应的 hwm 值已更改。