表格FAQ:Pika does not have any notion of threading in the code. If you want to
use Pika with threading, make sure you have a Pika connection per
thread, created in that thread. It is not safe to share one Pika
connection across threads,
所以让我们在线程内部创建连接:import pika
class PikaMassenger():
exchange_name = '...'
def __init__(self, *args, **kwargs):
self.conn = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
self.channel = self.conn.channel()
self.channel.exchange_declare(
exchange=self.exchange_name,
exchange_type='topic')
def consume(self, keys, callback):
result = self.channel.queue_declare('', exclusive=True)
queue_name = result.method.queue
for key in keys:
self.channel.queue_bind(
exchange=self.exchange_name,
queue=queue_name,
routing_key=key)
self.channel.basic_consume(
queue=queue_name,
on_message_callback=callback,
auto_ack=True)
self.channel.start_consuming()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.conn.close()
def start_consumer():
def callback(ch, method, properties, body):
print(" [x] %r:%r consumed" % (method.routing_key, body))
with PikaMassenger() as consumer:
consumer.consume(keys=[...], callback=callback)
consumer_thread = threading.Thread(target=start_consumer)
consumer_thread.start()