python - 为什么 ZeroMQ 示例不起作用?

标签 python zeromq

我是 Python/ZeroMQ 的新手,所以如果这是一个简单的问题,请表现出宽容。

我尝试运行一些示例,但效果不佳。
以下是 ZeroMQ 指南的 hwserver/hwclient 示例:

<小时/>

服务器

#   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
<小时/>

客户端

#    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))
<小时/>

我得到这个输出:

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

有人可以告诉我为什么我没有得到服务器的打印信息,例如“已收到请求”和“teeeest”吗?

谢谢!

最佳答案

why I don't get the prints of the server like "Received request" and "teeeest"?

嗯,
你确实明白了,
但是因为您没有查看正确的(两个)地方(如果还从服务器端 PyCharm/终端窗口阅读,您将看到“em——这是个好消息)。

欢迎来到 。让两个“角色”一起跳舞(如 REQ/REP 或其他 ZeroMQ 可扩展正式通信模式原型(prototype)中的编码),您必须检查 < strong>distributed-dancing 查看全貌。

只看一方面会让你看不到完整的故事。

为了使其更容易看到,让我们稍微修改一下代码:

<小时/>

服务器模式:

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
<小时/>

客户端模式:

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' ]

所以,
接下来只需查看另一个窗口/终端,
您将在其中看到所有预期的服务器端 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.......................
...
<小时/>

有兴趣尽快掌握 ZeroMQ 吗?

可以享受再阅读 5 秒
关于 [ ZeroMQ hierarchy in less than a five seconds ] 中的主要概念差异 或其他帖子和讨论。

关于python - 为什么 ZeroMQ 示例不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50692413/

相关文章:

python - 针对不同群体的滚动窗口

c++ - ZMQ经销商-路由器-经销商模式

python - 如何在单个 ZMQ 发送请求中同时发送图像(ndarray)和字符串数据

python - ZeroMQ 连接阻塞

c++ - ZeroMQ 与 fork 服务器中的所有子进程共享上下文

python - 如何键入提示返回类型取决于参数输入类型的函数?

python - 在 FormAlchemy 中不需要非 NULL 字段(允许空字符串)

python - 无法在 Python 中定义术语

python - 如何向 PyQt 中的事件循环发出自定义事件

c - ZMQ : Sync messages between APs