Skip to content

Commit 17d9b45

Browse files
committed
Starter code for unsync and trio.
1 parent 8aab136 commit 17d9b45

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/09-built-on-asyncio/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
unsync
2+
trio
3+
trio_asyncio
4+
5+
aiohttp
6+
aiodns
7+
cchardet
8+
requests
9+
bs4
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import math
2+
3+
4+
def do_math(start=0, num=10):
5+
pos = start
6+
k_sq = 1000 * 1000
7+
while pos < num:
8+
pos += 1
9+
math.sqrt((pos - k_sq)*(pos - k_sq))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import datetime
2+
import math
3+
import time
4+
import requests
5+
6+
7+
def main():
8+
t0 = datetime.datetime.now()
9+
10+
compute_some()
11+
compute_some()
12+
compute_some()
13+
download_some()
14+
download_some()
15+
download_some_more()
16+
download_some_more()
17+
wait_some()
18+
wait_some()
19+
wait_some()
20+
wait_some()
21+
22+
dt = datetime.datetime.now() - t0
23+
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
24+
25+
26+
def compute_some():
27+
print("Computing...")
28+
for _ in range(1, 10_000_000):
29+
math.sqrt(25 ** 25 + .01)
30+
31+
32+
def download_some():
33+
print("Downloading...")
34+
url = 'https://talkpython.fm/episodes/show/174/coming-into-python-from-another-industry-part-2'
35+
resp = requests.get(url)
36+
resp.raise_for_status()
37+
38+
text = resp.text
39+
40+
print("Downloaded (more) {:,} characters.".format(len(text)))
41+
42+
43+
def download_some_more():
44+
print("Downloading more ...")
45+
url = 'https://pythonbytes.fm/episodes/show/92/will-your-python-be-compiled'
46+
resp = requests.get(url)
47+
resp.raise_for_status()
48+
49+
text = resp.text
50+
51+
print("Downloaded {:,} characters.".format(len(text)))
52+
53+
54+
def wait_some():
55+
print("Waiting...")
56+
for _ in range(1, 1000):
57+
time.sleep(.001)
58+
59+
60+
if __name__ == '__main__':
61+
main()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import asyncio
2+
import datetime
3+
import math
4+
5+
import aiohttp
6+
import requests
7+
8+
9+
def main():
10+
t0 = datetime.datetime.now()
11+
12+
loop = asyncio.get_event_loop()
13+
14+
tasks = [
15+
loop.create_task(compute_some()),
16+
loop.create_task(compute_some()),
17+
loop.create_task(compute_some()),
18+
loop.create_task(download_some()),
19+
loop.create_task(download_some()),
20+
loop.create_task(download_some_more()),
21+
loop.create_task(download_some_more()),
22+
loop.create_task(wait_some()),
23+
loop.create_task(wait_some()),
24+
loop.create_task(wait_some()),
25+
loop.create_task(wait_some()),
26+
]
27+
28+
loop.run_until_complete(asyncio.gather(*tasks))
29+
30+
dt = datetime.datetime.now() - t0
31+
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
32+
33+
34+
async def compute_some():
35+
print("Computing...")
36+
for _ in range(1, 10_000_000):
37+
math.sqrt(25 ** 25 + .01)
38+
39+
40+
async def download_some():
41+
print("Downloading...")
42+
url = 'https://talkpython.fm/episodes/show/174/coming-into-python-from-another-industry-part-2'
43+
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
44+
async with session.get(url) as resp:
45+
resp.raise_for_status()
46+
47+
text = await resp.text()
48+
49+
print("Downloaded (more) {:,} characters.".format(len(text)))
50+
51+
52+
async def download_some_more():
53+
print("Downloading more ...")
54+
url = 'https://pythonbytes.fm/episodes/show/92/will-your-python-be-compiled'
55+
resp = requests.get(url)
56+
resp.raise_for_status()
57+
58+
text = resp.text
59+
60+
print("Downloaded {:,} characters.".format(len(text)))
61+
62+
63+
async def wait_some():
64+
print("Waiting...")
65+
for _ in range(1, 1000):
66+
await asyncio.sleep(.001)
67+
68+
69+
if __name__ == '__main__':
70+
main()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import asyncio
2+
import datetime
3+
import math
4+
5+
import aiohttp
6+
import requests
7+
8+
9+
def main():
10+
t0 = datetime.datetime.now()
11+
12+
loop = asyncio.get_event_loop()
13+
14+
tasks = [
15+
loop.create_task(compute_some()),
16+
loop.create_task(compute_some()),
17+
loop.create_task(compute_some()),
18+
loop.create_task(download_some()),
19+
loop.create_task(download_some()),
20+
loop.create_task(download_some_more()),
21+
loop.create_task(download_some_more()),
22+
loop.create_task(wait_some()),
23+
loop.create_task(wait_some()),
24+
loop.create_task(wait_some()),
25+
loop.create_task(wait_some()),
26+
]
27+
28+
loop.run_until_complete(asyncio.gather(*tasks))
29+
30+
dt = datetime.datetime.now() - t0
31+
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
32+
33+
34+
async def compute_some():
35+
print("Computing...")
36+
for _ in range(1, 10_000_000):
37+
math.sqrt(25 ** 25 + .01)
38+
39+
40+
async def download_some():
41+
print("Downloading...")
42+
url = 'https://talkpython.fm/episodes/show/174/coming-into-python-from-another-industry-part-2'
43+
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
44+
async with session.get(url) as resp:
45+
resp.raise_for_status()
46+
47+
text = await resp.text()
48+
49+
print("Downloaded (more) {:,} characters.".format(len(text)))
50+
51+
52+
async def download_some_more():
53+
print("Downloading more ...")
54+
url = 'https://pythonbytes.fm/episodes/show/92/will-your-python-be-compiled'
55+
resp = requests.get(url)
56+
resp.raise_for_status()
57+
58+
text = resp.text
59+
60+
print("Downloaded {:,} characters.".format(len(text)))
61+
62+
63+
async def wait_some():
64+
print("Waiting...")
65+
for _ in range(1, 1000):
66+
await asyncio.sleep(.001)
67+
68+
69+
if __name__ == '__main__':
70+
main()

0 commit comments

Comments
 (0)