Skip to content

Commit 413ee93

Browse files
committed
Final code for unsync demo.
1 parent 17d9b45 commit 413ee93

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/09-built-on-asyncio/the_unsync/core.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/09-built-on-asyncio/the_unsync/thesync.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unsync import unsync
12
import asyncio
23
import datetime
34
import math
@@ -9,34 +10,34 @@
910
def main():
1011
t0 = datetime.datetime.now()
1112

12-
loop = asyncio.get_event_loop()
13-
1413
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()),
14+
compute_some(),
15+
compute_some(),
16+
compute_some(),
17+
download_some(),
18+
download_some(),
19+
download_some_more(),
20+
download_some_more(),
21+
wait_some(),
22+
wait_some(),
23+
wait_some(),
24+
wait_some(),
2625
]
2726

28-
loop.run_until_complete(asyncio.gather(*tasks))
27+
[t.result() for t in tasks]
2928

3029
dt = datetime.datetime.now() - t0
3130
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
3231

3332

34-
async def compute_some():
33+
@unsync(cpu_bound=True)
34+
def compute_some():
3535
print("Computing...")
3636
for _ in range(1, 10_000_000):
3737
math.sqrt(25 ** 25 + .01)
3838

3939

40+
@unsync()
4041
async def download_some():
4142
print("Downloading...")
4243
url = 'https://talkpython.fm/episodes/show/174/coming-into-python-from-another-industry-part-2'
@@ -49,7 +50,8 @@ async def download_some():
4950
print("Downloaded (more) {:,} characters.".format(len(text)))
5051

5152

52-
async def download_some_more():
53+
@unsync()
54+
def download_some_more():
5355
print("Downloading more ...")
5456
url = 'https://pythonbytes.fm/episodes/show/92/will-your-python-be-compiled'
5557
resp = requests.get(url)
@@ -60,6 +62,7 @@ async def download_some_more():
6062
print("Downloaded {:,} characters.".format(len(text)))
6163

6264

65+
@unsync()
6366
async def wait_some():
6467
print("Waiting...")
6568
for _ in range(1, 1000):

0 commit comments

Comments
 (0)