快速提问:
我是否需要祈祷在与 RabbitMQ 通信时不要让两个独立的应用程序同时使用同一个 channel ,以防止出现“Broken Pipe Error”? (或者线程与拥有两个或多个独立的应用程序不同吗?)
历史:
我已经编写了一些应用程序,其中一个(io-server)从我的其他应用程序的角度来看就像一个服务器,从 RabbitMQ 服务器的角度来看就像一个客户端。
现在,在大约 10 分钟内,一切都按预期运行。然后,我的 io-server 崩溃了。这是回溯的最后一部分:
File "/usr/local/lib/python2.7/dist-packages/amqp-1.4.2-py2.7.egg/amqp/transport.py", line 163, in write_frame
frame_type, channel, size, payload, 0xce,
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 32] Broken pipe
这里是 RabbitMQ 日志的相关部分:
=ERROR REPORT==== 31-Mar-2014::12:29:53 ===
AMQP connection <0.22183.0> (running), channel 1 - error:
{amqp_error,unexpected_frame,
"expected content header for class 60, got non content header frame instead",
'basic.publish'}
=INFO REPORT==== 31-Mar-2014::12:30:23 ===
closing AMQP connection <0.22183.0> (127.0.0.1:43367 -> 127.0.0.1:5672)
据我在网上搜索,最有希望的答案是 here :
...
So yes, RabbitMQ closes the connection due to a connection-level error
with frame interleaving.
...
* Avoid publishing on *the same* channel from multiple threads
* Synchronize publishing in your own code
我可以让我的应用程序同步发布。但是我怎样才能保证我所有的应用程序同步运行呢?我真的必须这样做吗?
最佳答案
Do I need to pray for not having two independent applications use same channel
没有。这里的“ channel ”,您共享的链接是指共享的 Channel
对象。如果不同的应用程序(进程)不共享内存,它们将始终具有不同的 channel 。
Is threading different from having two or more independent applications?
是的。您使用的 amqp 库不是线程安全的。如果您在“IO 服务器”中的不同线程之间共享一个 Channel()
对象,这就是一个问题。但是,如果您的服务器是单线程的并且您运行许多并行实例,这将不是问题。
我建议您保持简单,不要在您的服务器应用程序中使用线程。假设您的 IO 服务器接受来自其他应用程序的 HTTP 连接;依赖nginx+uwsgi接受并行请求。
关于Python amqp 断管错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766765/