Skip to content

Commit 4a36255

Browse files
doc: ch8 - added more examples from anyio.
1 parent efbfab9 commit 4a36255

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import anyio
2+
3+
4+
async def producer(sender):
5+
async with sender:
6+
for value in range(5):
7+
await sender.send(value)
8+
await anyio.sleep(1)
9+
10+
11+
async def consumer(receiver):
12+
async with receiver:
13+
async for value in receiver:
14+
print(f'Got number {value}')
15+
await anyio.sleep(1)
16+
17+
18+
async def main():
19+
sender, receiver = anyio.create_memory_object_stream()
20+
async with anyio.create_task_group() as tg:
21+
async with sender, receiver:
22+
tg.start_soon(producer, sender.clone())
23+
tg.start_soon(consumer, receiver.clone())
24+
25+
26+
anyio.run(main)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pathlib import Path
2+
3+
import anyio
4+
from anyio import to_thread
5+
6+
7+
def print_dir_content():
8+
current_dir = Path.cwd()
9+
for file in current_dir.iterdir():
10+
print(file)
11+
12+
13+
async def display_dir_content(event):
14+
await to_thread.run_sync(print_dir_content)
15+
event.set()
16+
17+
18+
async def wait_print_finished(event):
19+
print('wait dir printing is finished')
20+
await event.wait()
21+
print('finished!')
22+
23+
24+
async def main():
25+
event = anyio.Event()
26+
async with anyio.create_task_group() as tg:
27+
tg.start_soon(display_dir_content, event)
28+
tg.start_soon(wait_print_finished, event)
29+
30+
31+
anyio.run(main)

docs/8. Introduction to AnyIO and Trio/index.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,18 @@ run(main, backend='trio')
9292
```
9393

9494

95-
## Integrating AnyIO and Trio into Existing Projects
96-
97-
98-
95+
And let's see a typical `consumer-producer` pattern in `anyio` which looks like `Trio`'s example:
96+
```python
97+
# ex_8_6
98+
{% include_relative ex_8_6.py %}
99+
```
99100

101+
If you want to do some blocking task, `anyio.to_thread` API seem do to a good job.
102+
One example would be to read the file names in the current path.
103+
[from this article](https://lewoudar.medium.com/anyio-all-you-need-for-async-programming-stuff-4cd084d0f6bd)
104+
```python
105+
# ex_8_7
106+
{% include_relative ex_8_7.py %}
107+
```
100108

101-
## Best Practices and Advanced Concepts
109+
## Integrating AnyIO and Trio into Existing Projects

docs/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ this repository has you covered.
7272
- Exploring Trio
7373
- Getting Started with AnyIO
7474
- Integrating AnyIO and Trio into Existing Projects
75-
- Best Practices and Advanced Concepts
7675

7776
9. * **Task Scheduling and Queues: Celery, APScheduler, and Beyond**
7877
- Introduction to Task Scheduling and Queues

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pytest~=8.3.3
22
httpx~=0.27.2
33
starlette==0.41.0
44
trio~=0.22.0
5+
anyio[trio]~=4.8.0

0 commit comments

Comments
 (0)