File: memory_transport.py

package info (click to toggle)
kombu 5.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,968 kB
  • sloc: python: 28,815; makefile: 318
file content (31 lines) | stat: -rw-r--r-- 755 bytes parent folder | download | duplicates (2)
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
30
31
"""
Example that use memory transport for message produce.
"""
from __future__ import annotations

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()