2011-09-12 09:25:42 +00:00
|
|
|
from kombu.pools import producers
|
|
|
|
|
2013-09-10 16:26:12 +00:00
|
|
|
from .queues import task_exchange
|
2011-09-12 09:25:42 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
priority_to_routing_key = {'high': 'hipri',
|
|
|
|
'mid': 'midpri',
|
|
|
|
'low': 'lopri'}
|
2011-09-12 09:25:42 +00:00
|
|
|
|
2011-09-12 09:47:22 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
def send_as_task(connection, fun, args=(), kwargs={}, priority='mid'):
|
|
|
|
payload = {'fun': fun, 'args': args, 'kwargs': kwargs}
|
2011-09-12 09:25:42 +00:00
|
|
|
routing_key = priority_to_routing_key[priority]
|
|
|
|
|
|
|
|
with producers[connection].acquire(block=True) as producer:
|
2013-01-17 13:50:01 +00:00
|
|
|
producer.publish(payload,
|
|
|
|
serializer='pickle',
|
|
|
|
compression='bzip2',
|
|
|
|
exchange=task_exchange,
|
2013-05-01 17:17:51 +00:00
|
|
|
declare=[task_exchange],
|
2013-01-17 13:50:01 +00:00
|
|
|
routing_key=routing_key)
|
2011-09-12 09:25:42 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from kombu import Connection
|
2013-09-10 16:26:12 +00:00
|
|
|
from .tasks import hello_task
|
2011-09-12 09:25:42 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
connection = Connection('amqp://guest:guest@localhost:5672//')
|
|
|
|
send_as_task(connection, fun=hello_task, args=('Kombu', ), kwargs={},
|
|
|
|
priority='high')
|