summaryrefslogtreecommitdiff
path: root/examples/memory_transport.py
blob: 8d667b818a6e5243f3ae5485e7dd8d7586df3090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Example that use memory transport for message produce.
"""
import time

from kombu import Connection, Consumer, Exchange, Queue

media_exchange = Exchange('media', 'direct')
video_queue = Queue('video', exchange=media_exchange, routing_key='video')
task_queues = [video_queue]


def handle_message(body, message):
    print(f"{time.time()} RECEIVED MESSAGE: {body!r}")
    message.ack()


connection = Connection("memory:///")
consumer = Consumer(connection, task_queues, callbacks=[handle_message])

producer = connection.Producer(serializer='json')
producer.publish(
    {"foo": "bar"},
    exchange=media_exchange,
    routing_key='video',
    declare=task_queues,
)
consumer.consume()
connection.drain_events()