Created
November 28, 2015 16:14
-
-
Save jterstriep/6975932a36abccdca9b7 to your computer and use it in GitHub Desktop.
Kombu ConsumerMixin Example (http://dhorrigan.com/post/57812373095/workers-and-image-processing)
This file contains hidden or 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
| class BaseWorker(ConsumerMixin): | |
| # The name of the queue that this worker consumes (see queues.py) | |
| worker_queue = None | |
| def __init__(self, connection, config): | |
| print "Worker Init" | |
| self.connection = connection | |
| self.config = config | |
| def get_consumers(self, Consumer, channel): | |
| return [Consumer(queues=queues[self.worker_queue], | |
| callbacks=[self.process])] | |
| def process(self, body, message): | |
| try: | |
| # This is where you process the message from the Queue | |
| except Exception, e: | |
| print "[%s] Processing Raised Exception: %s" % (body['id'], exc) | |
| # Acknowledge the message to the Queue | |
| message.ack() | |
| # Create the connection to the Queue server. | |
| # We use a with statement here so the connection is auto-closed | |
| with Connection(hostname=config['host'], | |
| port=config['port'], | |
| userid=config['user'], | |
| password=config['pass'], | |
| virtual_host=config['vhost']) as conn: | |
| try: | |
| BaseWorker(conn, config).run() | |
| except KeyboardInterrupt: | |
| print('Exiting') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment