logger = get_logger(__name__) class Worker(ConsumerMixin): def __init__(self, connection): self.connection = connection def get_consumers(self, Consumer, channel): return [Consumer(queues=task_queues, accept=['pickle', 'json'], callbacks=[self.process_task])] def process_task(self, body, message): logger.info('Got message: %s', body) message.ack() if __name__ == '__main__': with create_connection() as conn: try: worker = Worker(conn) worker.run() except KeyboardInterrupt: print('bye bye')
logger = get_logger(__name__) priority_to_routing_key = {'high': 'hipri', 'mid': 'midpri', 'low': 'lopri'} def send_as_task(connection, payload={}, kwargs={}, priority='mid'): routing_key = priority_to_routing_key[priority] with producers[connection].acquire(block=True) as producer: producer.publish(payload, serializer='json', compression='bzip2', exchange=task_exchange, declare=[task_exchange], routing_key=routing_key) if __name__ == '__main__': from tasks import hello_task connection = create_connection() logger.info (connection.info()) #for i in range (1, 10): payload = { 'message': 'Test message' } send_as_task(connection, payload=payload, kwargs={}, priority='high') # logger.info ("Message %d sent." % (i))