kombu/examples/simple_task_queue/client.py

30 lines
1.0 KiB
Python
Raw Normal View History

2011-09-12 09:25:42 +00:00
from __future__ import with_statement
from kombu.common import maybe_declare
from kombu.pools import producers
2011-09-12 09:47:22 +00:00
from queues import task_exchange
2011-09-12 09:25:42 +00:00
priority_to_routing_key = {"high": "hipri",
"mid": "midpri",
"low": "lopri"}
2011-09-12 09:47:22 +00:00
def send_as_task(connection, fun, args=(), kwargs={}, priority="mid"):
2011-09-12 09:25:42 +00:00
payload = {"fun": fun, "args": args, "kwargs": kwargs}
routing_key = priority_to_routing_key[priority]
with producers[connection].acquire(block=True) as producer:
maybe_declare(task_exchange, producer.channel)
producer.publish(payload, serializer="pickle",
2011-09-12 09:47:22 +00:00
compression="bzip2",
2011-09-12 09:25:42 +00:00
routing_key=routing_key)
if __name__ == "__main__":
from kombu import BrokerConnection
2011-09-12 09:47:22 +00:00
from tasks import hello_task
2011-09-12 09:25:42 +00:00
2011-09-12 09:47:22 +00:00
connection = BrokerConnection("amqp://guest:guest@localhost:5672//")
send_as_task(connection, fun=hello_task, args=("Kombu", ), kwargs={},
2011-09-12 09:25:42 +00:00
priority="high")