Created
October 10, 2014 13:52
-
-
Save vsudilov/eda0a23338a69bb728cd to your computer and use it in GitHub Desktop.
pika/rabbitMQ basic worker pattern
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
import pika | |
class GenericWorker: | |
def __init__(self,url='amqp://admin:password@localhost:5672/%2F'): | |
self.connection = pika.BlockingConnection(pika.URLParameters(url)) | |
self.channel = self.connection.channel() | |
self.channel.exchange_declare(exchange='TestExchange',exchange_type='direct',passive=False,durable=False) | |
self.channel.queue_declare(queue='TestQueue') | |
self.channel.queue_bind(queue='TestQueue',exchange='TestExchange',routing_key='TestQueueRoute') | |
def run(self): | |
self.channel.basic_consume(self.callback,queue='TestQueue') | |
self.channel.start_consuming() | |
def callback(self, channel, method_frame, header_frame, body): | |
print "Entered Callback with body=", body | |
print "Exited Callback gracefully" | |
def publish(self,payload): | |
self.channel.basic_publish('TestExchange','TestQueueRoute',payload) | |
W = GenericWorker() | |
W.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment