ZMQError:地址已在使用中(使用多处理创建动态套接字)

时间:2014-04-16 10:01:47

标签: python zeromq

在我的zmq项目中,我正在尝试为各种类型动态创建套接字。但是,当我运行此脚本时,获取"地址已使用错误"。

helpers.py

def create_connection(sock_type, address, port, \
                      connection_method, \
                      topic_filter=""):
"""
This fn helps to create a websocket according to the args
:sock_type = PUB, PUSH, PULL, SUB
:address = "127.0.0.1"
:port = 5376
:connection_method = bind or connect
:topic_filter = if the sock_type PULL, pass the topic filter

"""
context = zmq.Context()
ws = context.socket(SOCK_TYPES[sock_type])

if connection_method == 'bind':
    ws.bind("tcp://%s:%s" %(address, port))
else:
    ws.connect("tcp://%s:%s" %(address, port))

if sock_type == 'SUB':
    ws.setsockopt(zmq.SUBSCRIBE, topic_filter)

print "ws for ",sock_type, address, port, \
                      connection_method, \
                      topic_filter
return ws

module.py

from helpers import create_connection
def worker(pid):

        work_receiver = create_connection("PULL", WORKER_IP, WORKER_PORT, "connect")
        result_sender = create_connection("PUSH", RESULT_SENDER_IP, RESULT_SENDER_PORT, "bind")
        result_status_receiver = create_connection("SUB", RESULT_COLLECTOR_IP, RESULT_COLLECTOR_PORT, "connect")

     ......
     ......

for worker_id in range(10):
    Process(target=worker, args=(worker_id,)).start()

如果我们通过在工作模块中进行硬编码来创建它,而不是动态套接字创建,它似乎有效

settings.py

CONTROLLER_IP = "127.0.0.1"
CONTROLLER_PORT = 5676

WORKER_IP = "127.0.0.1"
WORKER_PORT = 5677

RESULT_SENDER_IP = "127.0.0.1"
RESULT_SENDER_PORT = 5672

RESULT_COLLECTOR_IP = "127.0.0.1"
RESULT_COLLECTOR_PORT = 5678 

错误

Process Process-2:
Traceback (most recent call last):
ws for  PULL 127.0.0.1 5677 connect 
  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 "master_updated.py", line 30, in worker
    result_sender = create_connection("PUSH", RESULT_SENDER_IP, RESULT_SENDER_PORT, "bind")
  File "/home/rlx-l004/Dropbox/github/zmq_eap/helpers.py", line 42, in create_connection
ws for  PULL 127.0.0.1 5677 connect 
    ws.bind("tcp://%s:%s" %(address, port))
  File "socket.pyx", line 448, in zmq.backend.cython.socket.Socket.bind (zmq/backend/cython/socket.c:4126)
  File "checkrc.pxd", line 21, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6174)
Process Process-4:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
ZMQError: Address already in use
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "master_updated.py", line 30, in worker

修改

顺序运行时没有错误

for i in range(10):
        worker(i)

0 个答案:

没有答案