File: publish_individually.py

package info (click to toggle)
python-aio-pika 9.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,460 kB
  • sloc: python: 8,003; makefile: 37; xml: 1
file content (36 lines) | stat: -rw-r--r-- 999 bytes parent folder | download | duplicates (3)
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
32
33
34
35
36
import asyncio
from typing import Generator

from aio_pika import Message, connect


def get_messages_to_publish() -> Generator[bytes, None, None]:
    for i in range(10000):
        yield f"Hello World {i}!".encode()


async def main() -> None:
    # Perform connection
    connection = await connect("amqp://guest:guest@localhost/")

    async with connection:
        # Creating a channel
        channel = await connection.channel()

        # Declaring queue
        queue = await channel.declare_queue("hello")

        # Sending the messages
        for msg in get_messages_to_publish():
            # Waiting for publisher confirmation with timeout for every message
            await channel.default_exchange.publish(
                Message(msg),
                routing_key=queue.name,
                timeout=5.0,
            )
        # Done sending messages
        print(" [x] Sent and confirmed multiple messages individually. ")


if __name__ == "__main__":
    asyncio.run(main())