Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pika RabbitMQ从消费者处发布_Python_Python 3.x_Rabbitmq_Pika_Python Pika - Fatal编程技术网

Python Pika RabbitMQ从消费者处发布

Python Pika RabbitMQ从消费者处发布,python,python-3.x,rabbitmq,pika,python-pika,Python,Python 3.x,Rabbitmq,Pika,Python Pika,我有一只兔子。我想让消费者进行一些消息处理,通过time.sleep(10)模拟,然后将消息发布到不同的队列。我知道消费者回调有一个频道,理论上可以用来发布,但这似乎是一个糟糕的实现,因为如果basic\u publish()以某种方式管理强制关闭频道,那么消费者就会死亡。处理这个问题的最佳方法是什么 import time import pika connection = pika.BlockingConnection( pika.ConnectionParameters(host=

我有一只兔子。我想让消费者进行一些消息处理,通过
time.sleep(10)
模拟,然后将消息发布到不同的队列。我知道消费者回调有一个频道,理论上可以用来发布,但这似乎是一个糟糕的实现,因为如果
basic\u publish()
以某种方式管理强制关闭频道,那么消费者就会死亡。处理这个问题的最佳方法是什么

import time
import pika

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='original_queue', exclusive=True)

channel.queue_bind(exchange='logs', queue='original_queue')

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    time.sleep(10)
    ch.basic_publish(exchange='logs', routing_key='different_queue', body='hello_world')

channel.basic_consume(
    queue='original_queue', on_message_callback=callback, auto_ack=True)

channel.start_consuming()

您可以以一种方式实现您的使用者:如果连接关闭,它将自动重新连接到RabbitMQ服务器。希望这能有所帮助(我没有在设计部分花太多心思,请随意提出一些建议!)

“basic_publish()以某种方式管理强制关闭频道”,此信息的来源在哪里?没有在文件中找到。
import time
import pika

reconnect_on_failure = True


def consumer(connection, channel):

    channel.exchange_declare(exchange='logs', exchange_type='fanout')
    result = channel.queue_declare(queue='original_queue', exclusive=True)
    channel.queue_bind(exchange='logs', queue='original_queue')
    print(' [*] Waiting for logs. To exit press CTRL+C')

    def callback(ch, method, properties, body):
        time.sleep(10)
        ch.basic_publish(exchange='logs', routing_key='different_queue', body='hello_world')

    channel.basic_consume(
  queue='original_queue', on_message_callback=callback, auto_ack=True)

    channel.start_consuming()


def get_connection_and_channel():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()


def start(reconnect_on_failure):
    connection, channel = get_connection_and_channel()
    consumer(connection, channel)
    # the if condition will be executed when the consumer's start_consuming loop exists
    if reconnect_on_failure:
        # cleanly close the connection and channel
        if not connection.is_closed():
            connection.close()
        if not channel.is_close():
            channel.close()
        start(reconnect_on_failure)


start(reconnect_on_failure)