暑期使用了rabbitmq,将其本地化后作为各组件的消息系统使用,现在记得当时在一个py里实现收发消息并通过websocket发送到服务端是可以正常运行的,但暑期的成果并没有记录下来,导致现在开学后,文件丢失或者启动方法忘记,用现有的文件和方法并不能正常运行,并且各类报错如下:
1.
ERROR:root:Failed to send message to RabbitMQ: Stream connection lost: IndexError('pop from an empty deque',)
Traceback (most recent call last):
File "webcon.py", line 53, in rabbit_send_message
self.channel1.basic_publish(exchange='', routing_key=self.queue_name, body=json.dumps(message))
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 2265, in basic_publish
self._flush_output()
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 1353, in _flush_output
self._connection._flush_output(lambda: self.is_closed, *waiters)
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 523, in _flush_output
raise self._closed_result.value.error
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 523, in _flush_output
raise self._closed_result.value.error
pika.exceptions.StreamLostError: Stream connection lost: IndexError('pop from an empty deque',)
2.
发生错误: Stream connection lost: IndexError('pop from an empty deque',)
Traceback (most recent call last):
File "webcon.py", line 134, in <module>
client.start()
File "webcon.py", line 127, in start
self.channel.start_consuming()
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 1883, in start_consuming
self._process_data_events(time_limit=None)
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 2044, in _process_data_events
self.connection.process_data_events(time_limit=time_limit)
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 842, in process_data_events
self._flush_output(common_terminator)
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 514, in _flush_output
self._impl.ioloop.poll()
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/select_connection.py", line 579, in poll
self._poller.poll()
File "/home/book/.local/lib/python3.6/site-packages/pika/adapters/select_connection.py", line 1184, in poll
events = self._poll.poll(self._get_max_wait())
AttributeError: 'NoneType' object has no attribute 'poll'
还有几种报错:attributeerror: 'nonetype' object has no attribute等,但出现次数最多的就是第一种:
pika.exceptions.StreamLostError: Stream connection lost: IndexError('pop from an empty deque',)
仔细看就会发现,这里提示队列里无消息,但有监听程序尝试从中消费信息出现错误,两个思路,要么是线程问题,要么是mq的设置问题,刚开始我以为是前者,所以去了解了pika线程互锁和线程池的方案,但后面依然会有这种报错,报错原因应该是后者。
对于Rabbitmq,如果你和我一样是采取的一个connection去执行监听和发布,那你一定要注意!Rabbitmq的监听消费会阻塞connection!(个人想法),导致发布消息失败或者监听消息失败哦。
最后通过创建两个connection或者分离到两py中,问题解决!
所以一定要了解一个库的基本工作原理~,不然就是bug里找bug,自寻shit路