-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_worker.py
37 lines (26 loc) · 1007 Bytes
/
task_worker.py
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
import asyncio
from aio_pika import connect
from aio_pika.abc import AbstractIncomingMessage
async def on_message(message: AbstractIncomingMessage) -> None:
async with message.process():
print(f" [x] Received message {message!r}")
await asyncio.sleep(message.body.count(b'.'))
print(f" Message body is: {message.body!r}")
async def main() -> None:
# Perform connection
connection = await connect("amqp://guest:guest@localhost/")
async with connection:
# Creating a channel
channel = await connection.channel()
await channel.set_qos(prefetch_count=1)
# Declaring queue
queue = await channel.declare_queue(
"task_queue",
durable=True,
)
# Start listening the queue with name 'task_queue'
await queue.consume(on_message)
print(" [*] Waiting for messages. To exit press CTRL+C")
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())