Skip to content

Instantly share code, notes, and snippets.

@naveed125
Created February 15, 2020 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveed125/f198ee49e08f046b13cca74e78c3cd5d to your computer and use it in GitHub Desktop.
Save naveed125/f198ee49e08f046b13cca74e78c3cd5d to your computer and use it in GitHub Desktop.
RabbitMQ based job worker python script.
import pika
import time
sleepTime = 10
print(' [*] Sleeping for ', sleepTime, ' seconds.')
time.sleep(30)
print(' [*] Connecting to server ...')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages.')
def callback(ch, method, properties, body):
print(" [x] Received %s" % body)
cmd = body.decode()
if cmd == 'hey':
print("hey there")
elif cmd == 'hello':
print("well hello there")
else:
print("sorry i did not understand ", body)
print(" [x] Done")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)
channel.start_consuming()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment