Pika和gevent的随机超时错误

时间:2012-06-28 08:25:50

标签: python rabbitmq gevent pika

我一直试图通过使用Pika库(由gevent修补的猴子)在我的gevent程序中使用RabbitMQ,gevent喜欢随机抛出超时错误。

我该怎么办?我可以使用另一个图书馆吗?

WARNING:root:Document not found, retrying primary.
Traceback (most recent call last):
  ...
  File "/usr/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 32, in __init__
    BaseConnection.__init__(self, parameters, None, reconnection_strategy)
  File "/usr/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 50, in __init__
    reconnection_strategy)
  File "/usr/lib/python2.7/dist-packages/pika/connection.py", line 170, in __init__
    self._connect()
  File "/usr/lib/python2.7/dist-packages/pika/connection.py", line 228, in _connect
    self.parameters.port or  spec.PORT)
  File "/usr/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 44, in _adapter_connect
    self._handle_read()
  File "/usr/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 151, in _handle_read
    data = self.socket.recv(self._suggested_buffer_size)
  File "/usr/lib/python2.7/dist-packages/gevent/socket.py", line 427, in recv
    wait_read(sock.fileno(), timeout=self.timeout, event=self._read_event)
  File "/usr/lib/python2.7/dist-packages/gevent/socket.py", line 169, in wait_read
    switch_result = get_hub().switch()
  File "/usr/lib/python2.7/dist-packages/gevent/hub.py", line 164, in switch
    return greenlet.switch(self)
timeout: timed out

2 个答案:

答案 0 :(得分:5)

Pika不适合与gevent一起使用,因为pika基于非阻塞套接字实现了自己与RabbitMQ的异步连接。这与gevent的实现并不相符。

您可以考虑使用py-amqplibkombu

答案 1 :(得分:0)

我在Django / Gunicorn应用程序中使用Pika时也遇到了超时问题。我玩了connection_attempts或增加了超时,但是RabbitMQ总是用握手错误关闭连接。后者似乎表明Pika从未在套接字上传输任何数据。

超时的原因可能就是libevent bug - 至少在我的环境中,附加到bug的脚本能够重现问题。

您可以尝试升级到gevent> = 1.0(在撰写时尚未发布):

wget http://gevent.googlecode.com/files/gevent-1.0b4.tar.gz
pip install gevent-1.0b4.tar.gz