Как метод handle_delivery реализован в pika Python?
Я пытаюсь понять этот пример кода из введения в pika ( http://pika.readthedocs.io/en/0.10.0/intro.html):
import pika
# Create a global channel variable to hold our channel object in
channel = None
# Step #2
def on_connected(connection):
"""Called when we are fully connected to RabbitMQ"""
# Open a channel
connection.channel(on_channel_open)
# Step #3
def on_channel_open(new_channel):
"""Called when our channel has opened"""
global channel
channel = new_channel
channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False, callback=on_queue_declared)
# Step #4
def on_queue_declared(frame):
"""Called when RabbitMQ has told us our Queue has been declared, frame is the response from RabbitMQ"""
channel.basic_consume(handle_delivery, queue='test')
# Step #5
def handle_delivery(channel, method, header, body):
"""Called when we receive a message from RabbitMQ"""
print body
# Step #1: Connect to RabbitMQ using the default parameters
parameters = pika.ConnectionParameters()
connection = pika.SelectConnection(parameters, on_connected)
try:
# Loop so we can communicate with RabbitMQ
connection.ioloop.start()
except KeyboardInterrupt:
# Gracefully close the connection
connection.close()
# Loop until we're fully closed, will stop on its own
connection.ioloop.start()
Согласно документации строки для handle_delivery
, он должен быть вызван, когда мы получим сообщение от RabbitMQ. Тем не менее, я не вижу этого handle_delivery
далее упоминается в коде, и я не нахожу его в исходном коде pika 0.10.0:
kurt@kurt-ThinkPad:~/dev/scratch/pika-0.10.0$ grep -r "handle_delivery"
kurt@kurt-ThinkPad:~/dev/scratch/pika-0.10.0$
Так как же handle_delivery
быть "поднятым" connection
?
1 ответ
step4 связывает канал с функцией handle_deivery.
# Step #4
def on_queue_declared(frame):
"""Called when RabbitMQ has told us our Queue has been declared, frame is the response from RabbitMQ"""
channel.basic_consume(handle_delivery, queue='test')
Канал связан с соединением на шаге № 2
# Step #2
def on_connected(connection):
"""Called when we are fully connected to RabbitMQ"""
# Open a channel
connection.channel(on_channel_open)