我在 Python 中使用带有 nanomsg 的 SURVEY 可扩展性协议。
我有一堆受访者插座,我将测量员连接到它们循环;然后,我等待surveyor.send_fd可读。我假设它至少连接到一位受访者,然后我发送调查。在受访者方面,我等待respondent.recv_fd可读,然后打电话respondent.recv()
了解调查情况,然后发送答案。回到surveyor,我等待surveyor.recv_fd可读,然后打电话询问 surveyor.recv()
受访者的答案。
有时我在尝试阅读受访者的答案时会出错:
Traceback (most recent call last):
[...]
nanomsg.NanoMsgAPIError: Operation cannot be performed in this state
我简化了代码以显示问题:
test_respondent.py
import nanomsg
import select
resp = nanomsg.Socket(nanomsg.RESPONDENT)
resp.bind("tcp://*:6542")
print 'waiting for respondent to be readable'
select.select([resp.recv_fd],[],[])
print 'receiving survey'
print resp.recv(flags=nanomsg.DONTWAIT)
print 'sending reply to surveyor'
resp.send("reply")
test_surveyor.py
import select
import nanomsg
s = nanomsg.Socket(nanomsg.SURVEYOR)
s.connect("tcp://127.0.0.1:6542")
print 'waiting for surveyor to be connected'
select.select([s.send_fd],[],[])
s.send("hello", nanomsg.DONTWAIT)
print "waiting for surveyor to be readable"
select.select([s.recv_fd],[],[])
print 'receiving reply from respondent'
print s.recv(flags=nanomsg.DONTWAIT)
测试.sh
python test_respondent.py &
python test_surveyor.py
在某些情况下,多次执行测试会导致上述异常。
知道我做错了什么吗?以及如何解决问题?