worker.py 678 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. import pika
  3. import time
  4. connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
  5. channel = connection.channel()
  6. channel.queue_declare(queue="task_queue", durable=True)
  7. print(" [*] Waiting for messages. To exit press CTRL+C")
  8. def callback(ch, method, properties, body):
  9. # instead of b'Hello World!'
  10. text = body.decode("utf-8")
  11. print(" [x] Received", text)
  12. time.sleep(text.count("."))
  13. print(" [x] Done")
  14. ch.basic_ack(delivery_tag=method.delivery_tag)
  15. channel.basic_qos(prefetch_count=1)
  16. channel.basic_consume(queue="task_queue", on_message_callback=callback)
  17. channel.start_consuming()
  18. connection.close()