-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbasic.py
More file actions
40 lines (29 loc) · 989 Bytes
/
basic.py
File metadata and controls
40 lines (29 loc) · 989 Bytes
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
"""
Basic example of using Taskiq with AioPika broker.
How to run:
1. Run worker: taskiq worker examples.basic:broker -w 1
2. Run broker: uv run examples/basic.py
"""
import asyncio
from taskiq_redis import RedisAsyncResultBackend
from taskiq_aio_pika import AioPikaBroker
broker = AioPikaBroker(
"amqp://guest:guest@localhost:5672/",
).with_result_backend(RedisAsyncResultBackend("redis://localhost:6379/0"))
@broker.task
async def add_one(value: int) -> int:
return value + 1
async def main() -> None:
await broker.startup()
# Send the task to the broker.
task = await add_one.kiq(1)
# Wait for the result.
result = await task.wait_result(timeout=2)
print(f"Task execution took: {result.execution_time} seconds.")
if not result.is_err:
print(f"Returned value: {result.return_value}")
else:
print("Error found while executing task.")
await broker.shutdown()
if __name__ == "__main__":
asyncio.run(main())