问题
I need to be able to gracefully stop a consumer (worker) who works in a Pika ioloop. The worker should stop after 60 seconds. Currently processed messages should be finished.
I tried to put a connection.close()
inside the callback function but that only stopped the current thread and not the complete ioloop. And it gave a terrible error output.
Please see line 16 and following in my code: I used the (basic example about Pika ioloop http://pika.github.com/connecting.html#cps-example:
from pika.adapters import SelectConnection
channel = None
def on_connected(connection):
connection.channel(on_channel_open)
def on_channel_open(new_channel):
global channel
channel = new_channel
channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False, callback=on_queue_declared)
def on_queue_declared(frame):
channel.basic_consume(handle_delivery, queue='test')
def handle_delivery(channel, method, header, body):
print body
# timer stuff which did NOT work
global start_time, timeout, connection
time_diff = time.time()-start_time
if time_diff > timeout:
#raise KeyboardInterrupt
connection.close()
timeout = 60
start_time = time.time()
connection = SelectConnection(parameters, on_connected)
try:
connection.ioloop.start()
except KeyboardInterrupt:
connection.close()
connection.ioloop.start()
回答1:
You can attach a timeout call-back function on the opened connection. Here is the extra code for your example.
timeout = 60
def on_timeout():
global connection
connection.close()
connection.add_timeout(timeout, on_timeout)
回答2:
You can try to use:
connection.ioloop.stop()
来源:https://stackoverflow.com/questions/8180596/set-timeout-for-pika-ioloop-async-rabbitmq