ZeroMQ ROUTER-DEALER重定向到另一个ROUTER-DEALER问题

时间:2016-07-21 02:02:27

标签: python sockets zeromq

我有一个名为vops-server.com的静态DNS,指向我的Dell PowerEdge 2950.在这2950上,我运行最初绑定到server.py的{​​{1}},然后将所有传入的客户端重定向到绑定到大于tcp://0.0.0.0:7777的端口的套接字,第一个是7777。这个新生成的7778对只是回显ROUTER-DEALER

Dell PowerEdge 2950运行"Hello, world!",无法正常运行代码,在KeyboardInterrupt上输出以下内容:

Ubuntu 16.04.1 LTS

客户输出以下内容:

k▒Eg
Listening on port 7778
Recv
Resource temporarily unavailable
Client on port 7778 disconnected
^CProcess Process-3:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "test_server.py", line 76, in worker_task
    data = worker_socket.recv_multipart()
  File "/usr/local/lib/python2.7/dist-packages/zmq/sugar/socket.py", line 395, in recv_multipart
    parts = [self.recv(flags, copy=copy, track=track)]
  File "zmq/backend/cython/socket.pyx", line 693, in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7283)
  File "zmq/backend/cython/socket.pyx", line 727, in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7081)
  File "zmq/backend/cython/socket.pyx", line 145, in zmq.backend.cython.socket._recv_copy (zmq/backend/cython/socket.c:2033)
  File "zmq/backend/cython/checkrc.pxd", line 12, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:7522)
    PyErr_CheckSignals()
KeyboardInterrupt

此时客户端应输出Connecting to distribution server tcp://vops-server.com:7777 [Dist] Send [Dist] Recv [Dist] 7778 Connecting to host tcp://vops-server.com:7778 [Host] Send [Host] Recv 的最后一行,但它等待接收消息。

现在,在运行[Host] Hello, world!的笔记本电脑上,输出符合预期:

Windows 10 Home 1511

客户端现在正确输出:

 Ç  )
Listening on port 7778
Recv
['\x00\x80\x00\x00)', 'Hello, world!']
Sent

请查看以下代码。

server.py:

Connecting to distribution server tcp://vops-server.com:7777
[Dist] Send
[Dist] Recv
[Dist] 7778
Connecting to host tcp://vops-server.com:7778
[Host] Send
[Host] Recv
[Host] Hello, world!

client.py:

import sys
import zmq
from multiprocessing import Process, Queue, Array, Value
import time

def server_task():
    port_base = 7777

    server_context = zmq.Context.instance()

    server_socket = server_context.socket(zmq.ROUTER)

    server_socket.bind("tcp://0.0.0.0:%d" % (port_base, ))

    timeout_queue = Queue()
    port_list = [ 1 ]

    proc_list = [ ]

    while True:
        try:
            client_id = server_socket.recv_multipart()[0]

            print(client_id)

            # Get an unused port from the list
            # Ports from clients that have timed out are recycled here

            while not timeout_queue.empty():
                port_list.append(timeout_queue.get())

            port_offset = port_list.pop()

            if len(port_list) == 0:
                port_list.append(port_offset + 1)

            # Spawn a new worker task, binding the port to a socket

            proc_running = Value("b", True)

            proc_list.append(proc_running)

            Process(target=worker_task, args=(proc_running, port_base, port_offset, timeout_queue)).start()

            # Send the new port to the client

            server_socket.send_multipart([client_id, str(port_base + port_offset)])

        except KeyboardInterrupt:
            break

    for proc_running in proc_list:
        proc_running.value = False

    server_socket.close()
    server_context.term()

def worker_task(proc_running, port_base, port_offset, timeout_queue):
    port = port_base + port_offset

    print("Listening on port %d" % (port, ))

    worker_context = zmq.Context.instance()

    worker_socket = worker_context.socket(zmq.ROUTER)

    worker_socket.setsockopt(zmq.RCVTIMEO, 5000)
    worker_socket.bind("tcp://0.0.0.0:%d" % (port, ))

    while proc_running.value:
        try:
            print("Recv")
            data = worker_socket.recv_multipart()

            print(data)
            worker_socket.send_multipart(data)

            print("Sent")
        except zmq.ZMQError as e:
            print(e)
            break

    print("Client on port %d disconnected" % (port, ))

    timeout_queue.put(port_offset)

    worker_socket.close()
    worker_context.term()

if __name__ == "__main__":
    server_task()

我无法理解为什么相同的代码在一台机器上工作而不在另一台机器上工作;我考虑在Dell PowerEdge 2950上安装Windows Server 2012,我希望错误在于操作系统,而不是硬件本身。现在我等着,希望某个地方的专家能解决我的问题。

1 个答案:

答案 0 :(得分:0)

相同的代码在运行Windows Server 2012 R2的Dell PowerEdge 2950上运行良好。 ZMQ似乎与Ubuntu有问题。