Python中的并发轮询问题

时间:2016-01-20 02:18:05

标签: python rabbitmq

这个问题困扰了我好几天了。我通过打开多个线程使用RabbitMQ运行多个消费者,我得到了以下错误。

File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 905, in queue_declare
    None, replies)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 1141, in _rpc
    self._wait_on_response(method_frame))
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 1162, in _send_method
    self.connection.process_data_events()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 240, in process_data_events
    if self._handle_read():
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 347, in _handle_read
    if self._read_poller.ready():
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 43, in inner
    return f(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 85, in ready
    events = self.poller.poll(self.poll_timeout)
RuntimeError: concurrent poll() invocation

很多Google搜索引导我Issue 8865,它描述了如何不能同时调用select.poll()函数。 并且以下脚本可以始终触发此错误:

import os
import select
import threading
import time


p = select.poll()
rfds = []
r, w = os.pipe()


for i in range(1000):
    fd = os.dup(r)
    rfds.append(fd)
    p.register(fd, select.POLLIN)

def resize_ufds_array():
    time.sleep(0.5)

    # trigger ufds array reallocation
    for fd in rfds:
        p.unregister(fd)
    p.register(w, select.POLLOUT)
    p.poll()

    # and make the call to poll() from the main thread return
    os.write(w, b'hello')

t = threading.Thread(target=resize_ufds_array)
t.start()
p.poll()

我发现此脚本无法触发从Window MSI安装的本地Python 2.7.8上的错误,因为select.poll已被删除。但是,在我的prod环境中,它的Ubuntu 14.04和我尝试从源代码安装Python 2.7.6-2.7.9,他们都有这个bug。知道如何在Ubuntu上解决这个问题吗?对于2.7版本here,这个问题有一个python修复但是在我应用补丁之后不知何故,脚本仍然显示错误。

0 个答案:

没有答案