Last active
March 19, 2018 15:17
-
-
Save rturowicz/5227877 to your computer and use it in GitHub Desktop.
pika
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# send to rabbitmq (celery task) | |
import pika | |
import simplejson | |
credentials = pika.credentials.PlainCredentials('user', 'pass') | |
connection = pika.BlockingConnection( | |
pika.ConnectionParameters( | |
host='localhost', | |
virtual_host='/vhost_name', | |
credentials=credentials | |
) | |
) | |
channel = connection.channel() | |
channel.exchange_declare(exchange='celery', type='direct', durable=True) | |
channel.basic_publish( | |
exchange='celery', | |
routing_key='celery', | |
body=simplejson.dumps({ | |
"id": "4cc7438e-afd4-4f8f-a2f3-f46567e7ca77", # should be unique | |
"task": "app.tasks.TestTask", | |
"args": [], | |
"kwargs": {'some_arg':'foo'} | |
}), | |
properties=pika.BasicProperties( | |
content_type="application/json", | |
delivery_mode=1 | |
) | |
) | |
connection.close() | |
# read from rabbitmq (celery task) | |
import pika | |
credentials = pika.credentials.PlainCredentials('user', 'pass') | |
connection = pika.BlockingConnection( | |
pika.ConnectionParameters( | |
host='localhost', | |
virtual_host='/vhost_name', | |
credentials=credentials | |
) | |
) | |
channel = connection.channel() | |
result = channel.queue_declare(queue='test_log', durable=True) | |
print ' [*] Waiting for logs. To exit press CTRL+C' | |
def callback(ch, method, properties, body): | |
print " [x] %r" % (body,) | |
ch.basic_ack(delivery_tag=method.delivery_tag) | |
channel.basic_consume( | |
callback, | |
queue='test_log', | |
no_ack=False | |
) | |
channel.basic_qos(prefetch_count=1) | |
channel.start_consuming() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment