RabbitMQ pika.exceptions.ConnectionClosed

时间:2016-05-19 10:53:33

标签: python rabbitmq pika

我尝试使用RabbitMQ发送消息和接收消息。我没有计算机科学背景,我使用的术语不太准确。

我尝试复制教程文件: 提交我的html表单时,我的python脚本(cgi)消息正在提交到队列

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='task_queue', durable=True)
        message = PN
        channel.basic_publish(exchange='',
                              routing_key='task_queue',
                              body=message,
                              properties=pika.BasicProperties(
                                 delivery_mode = 2, # make message persistent
                              ))
        connection.close()

我的接收器正在运行:

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] Received Project %r" % body)
    #ch.basic_ack(delivery_tag = method.delivery_tag) 
    if not (os.path.isfile(js_path)):
        print (' [*] ERROR files missing ')
        #ch.basic_ack(delivery_tag = method.delivery_tag)
        return
    p= subprocess.Popen(run a subprocess here)
    p.wait()

    print (' [*] Temporary Files removed')
    print(" [*] Waiting for messages. To exit press CTRL+C")

channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,queue='task_queue',no_ack=True)
channel.start_consuming()

它管理大部分时间但随机崩溃并出现以下错误:

  

回溯(最近一次呼叫最后一次):文件“Receive5.py”,第139行,in          channel.start_consuming()文件“C:\ Python27 \ lib \ site-packages \ pika \ adapters \ blocking_connection.py”,   第1681行,在start_consuming中       self.connection.process_data_events(time_limit = None)文件“C:\ Python27 \ lib \ site-packages \ pika \ adapters \ blocking_connection.py”,   第647行,在process_data_events中       self._flush_output(common_terminator)文件“C:\ Python27 \ lib \ site-packages \ pika \ adapters \ blocking_connection.py”,   第426行,在_flush_output中       raise exceptions.ConnectionClosed()pika.exceptions.ConnectionClosed

4 个答案:

答案 0 :(得分:12)

这是因为你保持主线程等待,因为这个pika无法处理传入的消息;在这种情况下,在子进程完成之前,它无法响应心跳。这会导致RabbitMQ认为客户端已死,并强制断开连接。

如果您希望使用心跳(建议使用),则需要定期呼叫connection.process_data_events。这可以通过添加一个循环来完成,该循环检查线程是否完成,并且每隔30s左右调用process_data_events直到线程完成。

答案 1 :(得分:1)

请添加此https://github.com/mosquito/aio-pika

这是一个asynchio包装器,如果你理解asynchron背后的概念很容易使用:)

答案 2 :(得分:0)

这是关于如何避免由于心跳而断开连接的 pika 文档。

https://pika.readthedocs.io/en/stable/examples/heartbeat_and_blocked_timeouts.html

在0.11.2以上的pika版本中,虽然我们可以在pika.ConnectionParameters里面加一个参数:heartbeat_interval=600,但是如果服务端的心跳值短到60s就无济于事了。 0.11.2以上的版本才能运行

答案 3 :(得分:-3)

似乎它没有在127.0.0.1:5672连接到RabbitMQ - 你确定RabbitMQ正在运行并正在监听127.0.0.1:5672吗?

for linux(debian)用户

您可以通过输入此命令来检查是否安装了RabbitMQ。

responseData.forEach(function(item) { if (expectedResult[item.deviceType] == null) { expectedResult[item.deviceType] = item; } else { expectedResult[item.deviceType].deviceCount += item.deviceCount; } });

如果您收到回复,则意味着它已安装。检查它是否正在运行?

启动rabbitMQ服务类型

sudo service rabbitmq-server status

重新启动rabbitMq使用此命令

sudo service rabbitmq-server start

没有响应意味着您没有安装Rabbitmq。通过键入来安装它 以下命令。

sudo service rabbitmq-server restart

sudo apt-get update

sudo apt-get -y upgrade

然后输入上面的start命令启动服务器。

相关问题