Skip to content

Commit 4e8fea2

Browse files
committed
Trio demo done.
1 parent ce6a05f commit 4e8fea2

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

src/09-built-on-asyncio/the_trio/prod_asyncio.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def main():
2020
loop.run_until_complete(final_task)
2121

2222
dt = datetime.datetime.now() - t0
23-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
23+
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(
24+
dt.total_seconds()), flush=True)
2425

2526

2627
async def generate_data(num: int, data: asyncio.Queue):
@@ -43,7 +44,8 @@ async def process_data(num: int, data: asyncio.Queue):
4344
dt = datetime.datetime.now() - t
4445

4546
print(colorama.Fore.CYAN +
46-
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
47+
" +++ Processed value {} after {:,.2f} sec.".format(
48+
value, dt.total_seconds()), flush=True)
4749
await asyncio.sleep(.5)
4850

4951

src/09-built-on-asyncio/the_trio/prod_sync.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def main():
1212
process_data(20, data)
1313

1414
dt = datetime.datetime.now() - t0
15-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
15+
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(
16+
dt.total_seconds()), flush=True)
1617

1718

1819
def generate_data(num: int, data: list):
@@ -38,7 +39,8 @@ def process_data(num: int, data: list):
3839
dt = datetime.datetime.now() - t
3940

4041
print(colorama.Fore.CYAN +
41-
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
42+
" +++ Processed value {} after {:,.2f} sec.".format(
43+
value, dt.total_seconds()), flush=True)
4244
time.sleep(.5)
4345

4446

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import datetime
2+
import colorama
3+
import random
4+
import trio
5+
6+
7+
async def main():
8+
t0 = datetime.datetime.now()
9+
print(colorama.Fore.WHITE + "App started.", flush=True)
10+
11+
data = trio.Queue(capacity=10)
12+
13+
with trio.move_on_after(5):
14+
async with trio.open_nursery() as nursery:
15+
nursery.start_soon(generate_data, 20, data, name='Prod 1')
16+
nursery.start_soon(generate_data, 20, data, name='Prod 2')
17+
nursery.start_soon(process_data, 40, data, name='Consumer')
18+
19+
dt = datetime.datetime.now() - t0
20+
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(
21+
dt.total_seconds()), flush=True)
22+
23+
24+
async def generate_data(num: int, data: trio.Queue):
25+
for idx in range(1, num + 1):
26+
item = idx*idx
27+
await data.put((item, datetime.datetime.now()))
28+
29+
print(colorama.Fore.YELLOW + f" -- generated item {idx}", flush=True)
30+
await trio.sleep(random.random() + .5)
31+
32+
33+
async def process_data(num: int, data: trio.Queue):
34+
processed = 0
35+
while processed < num:
36+
item = await data.get()
37+
38+
processed += 1
39+
value = item[0]
40+
t = item[1]
41+
dt = datetime.datetime.now() - t
42+
43+
print(colorama.Fore.CYAN +
44+
" +++ Processed value {} after {:,.2f} sec.".format(
45+
value, dt.total_seconds()), flush=True)
46+
await trio.sleep(.5)
47+
48+
49+
if __name__ == '__main__':
50+
trio.run(main)

0 commit comments

Comments
 (0)