Pika ioloopで働く消費者(労働者)を優雅に止めることができる必要があります。作業者は60秒後に停止する必要があります。現在処理中のメッセージは終了する必要があります。
コールバック関数の中に入れようとしましたconnection.close()
が、それは現在のスレッドを停止するだけで、完全なioloopを停止しませんでした。そして、それはひどいエラー出力を与えました。
私のコードの16行目以降を参照してください:(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()