我想从一个套接字向另一个套接字发送多条消息,我遇到了这个问题。
client.py
context = Context()
for i in range(10):
print(i)
out_socket = context.socket(REQ)
out_socket.connect("tcp://localhost:%s" % "5000")
message_content = ("hello", 1)
pickled_message = dumps(message_content)
out_socket.send(pickled_message, flags=NOBLOCK)
Server.py
context = Context()
in_socket = context.socket(REP)
in_socket.bind("tcp://*:%s" % "5000")
poller = Poller()
poller.register(in_socket, POLLIN)
while True:
socks = dict(poller.poll())
print(socks)
if socks.get(in_socket) == POLLIN:
pickled_received_message = in_socket.recv(flags=NOBLOCK)
received_message, sender_entity_id_e = loads(pickled_received_message)
print(received_message, sender_entity_id_e)
client.py的输出
client.py 输出从0到9打印的数字。这让我觉得邮件已发送且有效。然后我仔细查看了 server.py ,并意识到虽然对于循环运行了10次,但只发送了一条消息。
我想要实现的是从客户端向服务器发送多条消息而不会阻塞。任何想法如何实现这一目标?我已经尝试了几种解决方案而没有成我认为这应该有用,但我不确定它为什么不起作用。
答案 0 :(得分:0)
如果它说,异步服务在返回 .send( something, flags = zmq.DONTWAIT )
方法的非阻塞调用的那一刻不传递消息,那确实意味着ZeroMQ服务基础设施取得了消息有效负载的所有权(在此示例中为 something
),但没有更少,仅此而已。
接下来,同样的原则适用于 .bind()/.connect()
方法,这些方法开始构建下一个ZeroMQ基础架构AccessPoint,无论是本地(对于.bind()
- 案例),还是远程(如果.connect()
- 方法被要求开始做一些低级别的,特定于传输类的步骤和补救措施),其中一些可能在一段非常时间之后(只是基准测试,如何)在 [us]
中达到RTO状态需要很长时间,因为要求开始构建基础架构设置以进行粗略估计,在进入之前可能需要多长时间生产就绪状态......)
context = Context()
"""
for i in range( 10 ):
print( i ) # vvvvvvvvvvvvvvvvvvvv------------------- NEVER THIS WAY [us].....
out_socket = context.socket( REQ )
out_socket.connect( "tcp://localhost:%s" % "5000" )
message_content = ( "hello", 1 )
pass; pickled_message = dumps( message_content )
out_socket.send( pickled_message, flags = NOBLOCK )
"""
#-------------------------------------------------ZMQ-# <INSTANTIATION>
out_socket = context.socket( zmq.REQ ) # .SET AN INSTANCE
out_socket.setsockopt( zmq.LINGER, 0 ) # .SET ALWAYS
out_socket.setsockopt( zmq.SNDHWM, ... ) # .SET AS NEEDED
out_socket.setsockopt( zmq.AFFINITY, ... ) # .SET AS NEEDED
out_socket.setsockopt( zmq.<_*-attributes_>,...)# .SET AS NEEDED
#-------------------------------------------------ZMQ-# <INFRASTRUCTURE>
out_socket.connect( "tcp://localhost:%s" % "5000" ) # .CONNECT()-async call
for i in range( 100 ): # py2.7: xrange( 100 ):
print( i )
# -------------------------------------------------------
# pass; message_content = ( "hello", 1 )
# pickled_message = dumps(message_content)
# out_socket.send( pickled_message, flags = NOBLOCK )
#-------------------------------------------------------- PYTHON-SIDE COSTS
out_socket.send( dumps( ( "hello", i ) ), zmq.DONTWAIT )
print( "finished the for()-loops" )
#-------------------------------------------------ZMQ-# <GRACEFUL TERMINATION>
out_socket.close()
context.term()