File: publish_batches.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 (53 lines) | stat: -rw-r--r-- 1,538 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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")

        batchsize = 100
        outstanding_messages = []

        # Sending the messages
        for msg in get_messages_to_publish():
            outstanding_messages.append(
                asyncio.create_task(
                    channel.default_exchange.publish(
                        Message(msg),
                        routing_key=queue.name,
                        timeout=5.0,
                    )
                )
            )
            # Yield control flow to event loop, so message sending is initiated:
            await asyncio.sleep(0)

            if len(outstanding_messages) == batchsize:
                await asyncio.gather(*outstanding_messages)
                outstanding_messages.clear()

        if len(outstanding_messages) > 0:
            await asyncio.gather(*outstanding_messages)
            outstanding_messages.clear()
        # Done sending messages

        print(" [x] Sent and confirmed multiple messages in batches. ")


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