0
我有2個隊列,比如說q1和q2,它對應於e1和e2與綁定密鑰b1和b2交換。我想要並行運行消費者功能,比如c1和c2,它們將分別監聽q1和q2。我試了下面的方法:rabbitmq中的多個消費者爲多個隊列
def c1():
connection = pika.BlockingConnection(pika.ConnectionParameters(host=constants.rmqHostIp))
channel = connection.channel()
channel.exchange_declare(exchange='e1', durable='true',
type='topic')
result = channel.queue_declare(durable='false', queue='q1')
queue_name = result.method.queue
binding_key = "b1"
channel.queue_bind(exchange='e1',
queue=queue_name,
routing_key=binding_key)
channel.basic_consume(callback,queue=queue_name,no_ack=False)
channel.start_consuming()
def c2():
connection = pika.BlockingConnection(pika.ConnectionParameters(host=constants.rmqHostIp))
channel = connection.channel()
channel.exchange_declare(exchange='e2', durable='true',
type='topic')
result = channel.queue_declare(durable='false', queue='q2')
queue_name = result.method.queue
binding_key = "b2"
channel.queue_bind(exchange=e1,
queue=queue_name,
routing_key=binding_key)
channel.basic_consume(callback,queue=queue_name,no_ack=False)
channel.start_consuming()
if __name__ == '__main__':
c1()
c2()
但是,它只是偵聽c1函數和c2函數,它沒有被執行。我怎樣才能運行這兩個功能? 在此先感謝。
編輯:我有方法c1和在2個不同的模塊(文件)C1
你應該使用python線程模塊或一些替代阻止連接。 – alphiii