Skip to content

Commit ce6a05f

Browse files
committed
Starter code for trio demo.
1 parent 23d7f68 commit ce6a05f

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

Diff for: src/09-built-on-asyncio/.idea/dictionaries/screencaster.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/09-built-on-asyncio/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ aiodns
77
cchardet
88
requests
99
bs4
10+
colorama

Diff for: src/09-built-on-asyncio/the_trio/prod_asyncio.py

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

Diff for: src/09-built-on-asyncio/the_trio/prod_sync.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import datetime
2+
import colorama
3+
import random
4+
import time
5+
6+
7+
def main():
8+
t0 = datetime.datetime.now()
9+
print(colorama.Fore.WHITE + "App started.", flush=True)
10+
data = []
11+
generate_data(20, data)
12+
process_data(20, data)
13+
14+
dt = datetime.datetime.now() - t0
15+
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
16+
17+
18+
def generate_data(num: int, data: list):
19+
for idx in range(1, num + 1):
20+
item = idx*idx
21+
data.append((item, datetime.datetime.now()))
22+
23+
print(colorama.Fore.YELLOW + f" -- generated item {idx}", flush=True)
24+
time.sleep(random.random() + .5)
25+
26+
27+
def process_data(num: int, data: list):
28+
processed = 0
29+
while processed < num:
30+
item = data.pop(0)
31+
if not item:
32+
time.sleep(.01)
33+
continue
34+
35+
processed += 1
36+
value = item[0]
37+
t = item[1]
38+
dt = datetime.datetime.now() - t
39+
40+
print(colorama.Fore.CYAN +
41+
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
42+
time.sleep(.5)
43+
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)