File tree Expand file tree Collapse file tree 5 files changed +71
-6
lines changed
8. Introduction to AnyIO and Trio Expand file tree Collapse file tree 5 files changed +71
-6
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -92,10 +92,18 @@ run(main, backend='trio')
92
92
```
93
93
94
94
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
+ ```
99
100
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
+ ```
100
108
101
- ## Best Practices and Advanced Concepts
109
+ ## Integrating AnyIO and Trio into Existing Projects
Original file line number Diff line number Diff line change @@ -72,7 +72,6 @@ this repository has you covered.
72
72
- Exploring Trio
73
73
- Getting Started with AnyIO
74
74
- Integrating AnyIO and Trio into Existing Projects
75
- - Best Practices and Advanced Concepts
76
75
77
76
9 . * ** Task Scheduling and Queues: Celery, APScheduler, and Beyond**
78
77
- Introduction to Task Scheduling and Queues
Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ pytest~=8.3.3
2
2
httpx ~= 0.27.2
3
3
starlette == 0.41.0
4
4
trio ~= 0.22.0
5
+ anyio [trio ]~= 4.8.0
You can’t perform that action at this time.
0 commit comments