kombu/examples/complete_send.py

37 lines
1.1 KiB
Python
Raw Permalink Normal View History

"""
Example producer that sends a single message and exits.
You can use `complete_receive.py` to receive the message sent.
"""
from __future__ import annotations
from kombu import Connection, Exchange, Producer, Queue
#: By default messages sent to exchanges are persistent (delivery_mode=2),
#: and queues and exchanges are durable.
exchange = Exchange('kombu_demo', type='direct')
queue = Queue('kombu_demo', exchange, routing_key='kombu_demo')
with Connection('amqp://guest:guest@localhost:5672//') as connection:
2011-09-07 14:21:38 +00:00
#: Producers are used to publish messages.
2016-08-01 20:45:21 +00:00
#: a default exchange and routing key can also be specified
2011-09-07 14:21:38 +00:00
#: as arguments the Producer, but we rather specify this explicitly
#: at the publish call.
producer = Producer(connection)
2011-09-07 14:21:38 +00:00
#: Publish the message using the json serializer (which is the default),
#: and zlib compression. The kombu consumer will automatically detect
#: encoding, serialization and compression used and decode accordingly.
producer.publish(
{'hello': 'world'},
exchange=exchange,
routing_key='kombu_demo',
serializer='json',
compression='zlib',
)