I am new in Python/ ZeroMQ, so show forbearance if it is an easy question.
I try to run some examples, but it does not work really good.
Here is the hwserver/hwclient example of the ZeroMQ-Guide:
SERVER
# Hello World server in Python
# Binds REP socket to tcp://*:5555
# Expects b"Hello" from client, replies with b"World"
#
import time
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
message = socket.recv() # Wait for next request from client
print("Received request: %s" % message)
time.sleep(1) # Do some 'work'
print( "teeest" )
socket.send(b"World") # Send reply back to client
CLIENT
# Hello World client in Python
# Connects REQ socket to tcp://localhost:5555
# Sends "Hello" to server, expects "World" back
#
import zmq
context = zmq.Context()
print("Connecting to hello world server…") # Socket to talk to server
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
for request in range(10): # Do 10 requests,
# # waiting each time for a response
print("Sending request %s …" % request)
socket.send(b"Hello")
message = socket.recv() # Get the reply.
print("Received reply %s [ %s ]" % (request, message))
And I am getting this output:
Connecting to hello world server…
Sending request 0 …
Received reply 0 [ b'World' ]
Sending request 1 …
Received reply 1 [ b'World' ]
Sending request 2 …
Received reply 2 [ b'World' ]
Sending request 3 …
Received reply 3 [ b'World' ]
Sending request 4 …
Received reply 4 [ b'World' ]
Sending request 5 …
Received reply 5 [ b'World' ]
Sending request 6 …
Received reply 6 [ b'World' ]
Sending request 7 …
Received reply 7 [ b'World' ]
Sending request 8 …
Received reply 8 [ b'World' ]
Sending request 9 …
Received reply 9 [ b'World' ]
Process finished with exit code 0
Could somebody tell me why I dont get the prints of the server like "Received request" and "teeeest"?
Thank you!
答案 0 :(得分:1)
为什么 我没有得到服务器的打印件,如“收到的请求”和“teeeest”?
那么,
你实际上得到了他们,
但是因为你没有看到正确的(两个)地方(s)(如果也从服务器端的PyCharm / Terminal窗口读取,你会看到'em - 这是一个好消息)。
欢迎使用 distributed-computing 。有两个“角色”一起跳舞(在 REQ/REP
或其他ZeroMQ可扩展形式通信模式原型中编码),你必须检查分布式舞蹈的两面看全貌。
只观看一方让你没有完整的故事。
为了更容易看到这一点,让我们稍微修改一下代码:
SERVER MOD:
import time
import zmq
context = zmq.Context()
socket = context.socket( zmq.REP ); socket.bind( "tcp://*:5555" )
while True:
message = socket.recv() # Wait for next request from client
print( "TERMINAL[1]: SERVER has received request: %s" % message )
time.sleep(1) # Do some 'work'
print( "TERMINAL[1]: SERVER puts teeest......................." )
socket.send( b"World" ) # Send reply back to client
客户MOD:
import zmq
context = zmq.Context()
print( "TERMINAL[2] CLIENT says Connecting to hello world server…" )
socket = context.socket( zmq.REQ ) # Socket to talk to server
socket.connect( "tcp://localhost:5555" )
for request in range( 10 ): # Do 10 requests,
# # waiting each time for a response
print( "TERMINAL[2] CLIENT Sending request %s …" % request )
socket.send( b"Hello" )
message = socket.recv() # Get the reply.
print( "TERMINAL[2] CLIENT Received reply %s [ %s ]" % ( request, message ) )
这个输出你已经“知道”了:
TERMINAL[2] CLIENT says Connecting to hello world server…
TERMINAL[2] CLIENT Sending request 0 …
TERMINAL[2] CLIENT Received reply 0 [ b'World' ]
TERMINAL[2] CLIENT Sending request 1 …
TERMINAL[2] CLIENT Received reply 1 [ b'World' ]
TERMINAL[2] CLIENT Sending request 2 …
TERMINAL[2] CLIENT Received reply 2 [ b'World' ]
TERMINAL[2] CLIENT Sending request 3 …
TERMINAL[2] CLIENT Received reply 3 [ b'World' ]
TERMINAL[2] CLIENT Sending request 4 …
TERMINAL[2] CLIENT Received reply 4 [ b'World' ]
TERMINAL[2] CLIENT Sending request 5 …
TERMINAL[2] CLIENT Received reply 5 [ b'World' ]
TERMINAL[2] CLIENT Sending request 6 …
TERMINAL[2] CLIENT Received reply 6 [ b'World' ]
TERMINAL[2] CLIENT Sending request 7 …
TERMINAL[2] CLIENT Received reply 7 [ b'World' ]
TERMINAL[2] CLIENT Sending request 8 …
TERMINAL[2] CLIENT Received reply 8 [ b'World' ]
TERMINAL[2] CLIENT Sending request 9 …
TERMINAL[2] CLIENT Received reply 9 [ b'World' ]
所以,
接下来只是看看另一个窗口/终端,
您将在哪里看到所有预期的SERVER端print()
- s:
TERMINAL[1]: SERVER has received request: b'Hello'
TERMINAL[1]: SERVER puts teeest.......................
TERMINAL[1]: SERVER has received request: b'Hello'
TERMINAL[1]: SERVER puts teeest.......................
TERMINAL[1]: SERVER has received request: b'Hello'
TERMINAL[1]: SERVER puts teeest.......................
TERMINAL[1]: SERVER has received request: b'Hello'
TERMINAL[1]: SERVER puts teeest.......................
TERMINAL[1]: SERVER has received request: b'Hello'
TERMINAL[1]: SERVER puts teeest.......................
TERMINAL[1]: SERVER has received request: b'Hello'
TERMINAL[1]: SERVER puts teeest.......................
...
可以享受再进行5秒阅读 关于 [ ZeroMQ hierarchy in less than a five seconds ] 或其他帖子和讨论中的主要概念差异。
答案 1 :(得分:0)
客户端不应该打印teeest
而是打印服务器,因为print( "teeest" )
被指示在服务器文件中执行。
您的输出显示客户端的输出,但不显示服务器的输出。