无法在绑定了 socket.bind() 的套接字上发送消息

Can't send a message on a socket bound with socket.bind()

我正在尝试编写一个程序来接收数据包,并使用 socket.send() 方法将其发送到另一个端口。当我尝试发送消息(带有数据包副本)时,它什么也做不了。不过数据包收到很好。

import time
import zmq


context = zmq.Context()

# Socket facing out
push = context.socket(zmq.REQ)
# push.hwm = 1
push.bind("tcp://*:5556")

# Socket facing in
sub = context.socket(zmq.SUB)
sub.bind("tcp://*:5555")

while True:

    sub.setsockopt_string(zmq.SUBSCRIBE, "")
    msg = sub.recv()
    print("test")
    push.send_string("test")
    print("test2")

当我取消注释 push.hwm = 1 时它没有帮助。

让我们先揭秘代码:

import time
import zmq

context = zmq.Context()

push = context.socket( zmq.REQ ) # Socket facing out - THE MOST COMPLICATED ARCHETYPE
# push.hwm = 1                   #        a .hwm() ? - A STRANGE PROMISE IN v17/18
push.bind( "tcp://*:5556" )      # push acquires all ports 5556 to be REQ-served

sub = context.socket( zmq.SUB )  # Socket facing in  + SUB TOPIC LIST MANAGEMENT COSTS
sub.bind(  "tcp://*:5555" )      # sub  acquires all ports 5555 to be SUB-served

while True:

    sub.setsockopt_string( zmq.SUBSCRIBE, "" ) # /NEVER/.SET INFINITELY MANY TIMES?
    msg = sub.recv()                           # /NEVER/ USE A BLOCKING .recv()
    pass;                      print( "test" ) #         CLI - GUI STUB print()
    push.send_string( "test" )                 #                        .send()
    pass;                      print( "test2" )#         CLI - GUI STUB print()

除了使用 push.hwm 的一个相当奇怪的承诺外,它在 v3.+ 之前的 Python 中不起作用,并且有点“扭曲”/“扩展”本机 ZeroMQ API:

>> print pull.set_hwm.__doc__
Set the High Water Mark.
        
        On libzmq ≥ 3, this sets both SNDHWM and RCVHWM


        .. warning::

            New values only take effect for subsequent socket
            bind/connects.

应该首先重构这段代码。 REQ/REP 在这里根本没有用,没有足够强大的协调 dFSA 对应物(在删除概念性 REQ 之后,曾经将任何 REP 发送回已发送的 REQ - 从未尝试过 .recv() 任何此类 REP-answer 的自我自杀,对于一个案例,它偶尔确实是从一个未知的,如果曾经存在的,远程交互的 dFSA-node 发送的)。

可能变成这样的东西:

import time
import zmq

ms_TO_WAIT_IN_POLL = 100

def cliShow( aText = "no parameter was fed in" ):
    print( "{0:}:: {1:}".format( time.ctime(), aText ) )

context = zmq.Context()

push = context.socket( zmq.PUSH )# Socket facing out - THE zmq.PUSH ARCHETYPE is a right-enough one
push.bind( "tcp://*:5556" )      # push acquires all ports 5556 to be PUSH-served
push.setsockopt( zmq.LINGER, 0 ) # .SET always explicitly, even if "defaults" promise

pull = context.socket( zmq.PULL )# Socket facing in  - THE zmq.PULL ARCHETYPE is a right-enough one
pull.bind( "tcp://*:5555" )      # pull acquires all ports 5555 to be PULL-served
pull.setsockopt( zmq.LINGER, 0 ) # .SET always explicitly, even if "defaults" promise

try:
    while True:

        if ( 0 == pull.poll( ms_TO_WAIT_IN_POLL, zmq.POLLIN ) ):
           cliShow( "No message arrived yet" )     #         CLI - GUI STUB print()
        else:
           msg  = pull.recv( zmq.NOBLOCK )         # /NEVER/ USE A BLOCKING .recv()
           cliShow( "test" )                       #         CLI - GUI STUB print()
    
           push.send_string( "test" )              #                        .send()
           cliShow( "test2" )                      #         CLI - GUI STUB print()

except KeyboardInterrupt:
    pass

except:
    pass

finally:

    pull.close()
    push.close()

    context.term()