123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import pika
- import time
- connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
- channel = connection.channel()
- channel.queue_declare(queue="task_queue", durable=True)
- print(" [*] Waiting for messages. To exit press CTRL+C")
- heart = 0
- def callback(ch, method, properties, body):
- global heart
-
- text = body.decode("utf-8")
- print(" [x] Received", text)
- time.sleep(text.count("."))
- print(" [x] Done")
- heart = time.time()
- ch.basic_ack(delivery_tag=method.delivery_tag)
- channel.basic_qos(prefetch_count=1)
- channel.basic_consume(queue="task_queue", on_message_callback=callback)
- delay = 60
- while channel._consumer_infos:
- channel.connection.process_data_events(time_limit=delay)
- print("delay=", delay, "heart=", heart, "clock=", time.time());
- if ( heart == 0 ):
- if delay != 60:
- delay = 60
- print("Deep sleep...")
- else:
- delay = 10
- delta = time.time() - heart
- print("delta=", delta)
- if (delta > 50):
- print("ZZZzzz...")
- heart = 0
- connection.close()
|