Skip to content

Commit 398f7f7

Browse files
committed
Merge tag '0.1.2' into develop
0.1.2
2 parents e500bcb + 76da3b7 commit 398f7f7

36 files changed

+1435
-236
lines changed

.flake8

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ ignore =
8080
WPS229,
8181
; Found function with too much cognitive complexity
8282
WPS231,
83+
; Found too deep nesting
84+
WPS220,
85+
; Found line with high Jones Complexity
86+
WPS221,
87+
; function name should be lowercase
88+
N802,
89+
; Do not perform function calls in argument defaults.
90+
B008,
8391

8492
; all init files
8593
__init__.py:
@@ -99,6 +107,10 @@ per-file-ignores =
99107
WPS432,
100108
; Missing parameter(s) in Docstring
101109
DAR101,
110+
; Found too short name
111+
WPS111,
112+
; Found complex default value
113+
WPS404,
102114

103115
exclude =
104116
./.git,

docs/examples/introduction/aio_pika_broker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async def main() -> None:
2222
print(f"Returned value: {result.return_value}")
2323
else:
2424
print("Error found while executing task.")
25+
await broker.shutdown()
2526

2627

2728
if __name__ == "__main__":

docs/examples/introduction/full_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async def main() -> None:
2626
print(f"Returned value: {result.return_value}")
2727
else:
2828
print("Error found while executing task.")
29+
await broker.shutdown()
2930

3031

3132
if __name__ == "__main__":

docs/examples/introduction/inmemory_run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async def add_one(value: int) -> int:
1212

1313

1414
async def main() -> None:
15+
await broker.startup()
1516
# Send the task to the broker.
1617
task = await add_one.kiq(1)
1718
# Wait for the result.
@@ -21,6 +22,7 @@ async def main() -> None:
2122
print(f"Returned value: {result.return_value}")
2223
else:
2324
print("Error found while executing task.")
25+
await broker.shutdown()
2426

2527

2628
if __name__ == "__main__":
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
from typing import AsyncGenerator
3+
4+
from taskiq import TaskiqDepends
5+
6+
7+
async def dependency() -> AsyncGenerator[str, None]:
8+
print("Startup")
9+
await asyncio.sleep(0.1)
10+
11+
yield "value"
12+
13+
await asyncio.sleep(0.1)
14+
print("Shutdown")
15+
16+
17+
async def my_task(dep: str = TaskiqDepends(dependency)) -> None:
18+
print(dep.upper())
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from taskiq import TaskiqDepends
2+
3+
4+
async def db_connection() -> str:
5+
return "let's pretend as this is a connection"
6+
7+
8+
class MyDAO:
9+
def __init__(self, db_conn: str = TaskiqDepends(db_connection)) -> None:
10+
self.db_conn = db_conn
11+
12+
def get_users(self) -> str:
13+
return self.db_conn.upper()
14+
15+
16+
def my_task(dao: MyDAO = TaskiqDepends()) -> None:
17+
print(dao.get_users())
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import random
2+
3+
from taskiq import TaskiqDepends
4+
5+
6+
def common_dep() -> int:
7+
# For example it returns 8
8+
return random.randint(1, 10)
9+
10+
11+
def dep1(cd: int = TaskiqDepends(common_dep)) -> int:
12+
# This function will return 9
13+
return cd + 1
14+
15+
16+
def dep2(cd: int = TaskiqDepends(common_dep)) -> int:
17+
# This function will return 10
18+
return cd + 2
19+
20+
21+
def my_task(
22+
d1: int = TaskiqDepends(dep1),
23+
d2: int = TaskiqDepends(dep2),
24+
) -> int:
25+
# This function will return 19
26+
return d1 + d2

docs/examples/state/events_example.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import asyncio
2+
from typing import Optional
3+
4+
from redis.asyncio import ConnectionPool, Redis # type: ignore
5+
from taskiq_aio_pika import AioPikaBroker
6+
from taskiq_redis import RedisAsyncResultBackend
7+
8+
from taskiq import Context, TaskiqDepends, TaskiqEvents, TaskiqState
9+
10+
# To run this example, please install:
11+
# * taskiq
12+
# * taskiq-redis
13+
# * taskiq-aio-pika
14+
15+
broker = AioPikaBroker(
16+
"amqp://localhost",
17+
result_backend=RedisAsyncResultBackend(
18+
"redis://localhost/0",
19+
),
20+
)
21+
22+
23+
@broker.on_event(TaskiqEvents.WORKER_STARTUP)
24+
async def startup(state: TaskiqState) -> None:
25+
# Here we store connection pool on startup for later use.
26+
state.redis = ConnectionPool.from_url("redis://localhost/1")
27+
28+
29+
@broker.on_event(TaskiqEvents.WORKER_SHUTDOWN)
30+
async def shutdown(state: TaskiqState) -> None:
31+
# Here we close our pool on shutdown event.
32+
await state.redis.disconnect()
33+
34+
35+
@broker.task
36+
async def get_val(key: str, context: Context = TaskiqDepends()) -> Optional[str]:
37+
# Now we can use our pool.
38+
redis = Redis(connection_pool=context.state.redis, decode_responses=True)
39+
return await redis.get(key)
40+
41+
42+
@broker.task
43+
async def set_val(key: str, value: str, context: Context = TaskiqDepends()) -> None:
44+
# Now we can use our pool to set value.
45+
await Redis(connection_pool=context.state.redis).set(key, value)
46+
47+
48+
async def main() -> None:
49+
await broker.startup()
50+
51+
set_task = await set_val.kiq("key", "value")
52+
set_result = await set_task.wait_result(with_logs=True)
53+
if set_result.is_err:
54+
print(set_result.log)
55+
raise ValueError("Cannot set value in redis. See logs.")
56+
57+
get_task = await get_val.kiq("key")
58+
get_res = await get_task.wait_result()
59+
print(f"Got redis value: {get_res.return_value}")
60+
61+
await broker.shutdown()
62+
63+
64+
if __name__ == "__main__":
65+
asyncio.run(main())

docs/examples/state/generator_deps.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Generator
2+
3+
from taskiq import TaskiqDepends
4+
5+
6+
def dependency() -> Generator[str, None, None]:
7+
print("Startup")
8+
9+
yield "value"
10+
11+
print("Shutdown")
12+
13+
14+
async def my_task(dep: str = TaskiqDepends(dependency)) -> None:
15+
print(dep.upper())

docs/examples/state/no_cache.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import random
2+
3+
from taskiq import TaskiqDepends
4+
5+
6+
def common_dep() -> int:
7+
return random.randint(1, 10)
8+
9+
10+
def dep1(cd: int = TaskiqDepends(common_dep)) -> int:
11+
return cd + 1
12+
13+
14+
def dep2(cd: int = TaskiqDepends(common_dep, use_cache=False)) -> int:
15+
return cd + 2
16+
17+
18+
def my_task(
19+
d1: int = TaskiqDepends(dep1),
20+
d2: int = TaskiqDepends(dep2),
21+
) -> int:
22+
return d1 + d2

0 commit comments

Comments
 (0)