Skip to content

Commit f7e9264

Browse files
committed
updated some async examples
1 parent 47c8d48 commit f7e9264

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

source/examples/async/async_executor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ async def slow_task():
2626
# This will block for 2-10 seconds!
2727
# result = slow_function(random.random() * 8 + 2)
2828
# uncomment to put it on a different thread:
29-
result = await loop.run_in_executor(None,
30-
slow_function,
31-
random.random() * 8 + 2)
29+
result = slow_function(random.random() * 8 + 2)
30+
#result = await loop.run_in_executor(None,
31+
# slow_function,
32+
# random.random() * 8 + 2)
3233
print("slow function done: result", result)
33-
#await asyncio.sleep(0.0) # to release the loop
34+
await asyncio.sleep(0.0) # to release the loop
3435

3536

3637
def slow_function(duration):

source/examples/async/async_timer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def shutdown():
4141
# or add tasks to the loop like this:
4242
loop.create_task(display_date(3))
4343
loop.create_task(display_date(4))
44+
for i in range(5, 20):
45+
loop.create_task(display_date(i))
4446

4547
# this will shut the event loop down in 15 seconds
4648
loop.call_later(15, shutdown)

source/examples/async/gather.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111

1212
import asyncio
1313

14+
1415
async def factorial(name, number):
1516
f = 1
16-
for i in range(2, number+1):
17+
for i in range(2, number + 1):
1718
print("Task %s: Compute factorial(%s)..." % (name, i))
18-
await asyncio.sleep(1)
19+
await asyncio.sleep(1) # to simulate a longer process...
1920
f *= i
2021
print("Task %s: factorial(%s) = %s" % (name, number, f))
2122

2223
loop = asyncio.get_event_loop()
23-
loop.run_until_complete(asyncio.gather(
24-
factorial("A", 2),
25-
factorial("B", 3),
26-
factorial("C", 4),
27-
))
24+
loop.run_until_complete(asyncio.gather(factorial("A", 2),
25+
factorial("B", 3),
26+
factorial("C", 4),
27+
))
2828
loop.close()

source/examples/async/get_news_sync.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
def get_sources():
2626
"""
27-
get all the english language sources of news
27+
Get all the english language sources of news
2828
2929
'https://newsapi.org/v1/sources?language=en'
3030
"""
@@ -33,6 +33,8 @@ def get_sources():
3333
resp = requests.get(url, params=params)
3434
data = resp.json()
3535
sources = [src['id'].strip() for src in data['sources']]
36+
print("all the sources")
37+
print(sources)
3638
return sources
3739

3840

source/examples/async/task_loop.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Example code for making the very simplest event loop possible
5+
6+
Inspired by Brett Cannon's "How the heck does async/await work in Python 3.5?":
7+
8+
https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5
9+
10+
and André Caron's "A tale of event loops":
11+
12+
https://github.com/AndreLouisCaron/a-tale-of-event-loops
13+
14+
I'm calling this a "task loop" rather than an event loop, because it really is
15+
only designed to run a bunch of tasks asynchronously, wohtout the other
16+
machinery required to support proper events -- like a way to generate a new
17+
event, for instance.
18+
"""
19+
20+

source/examples/async/ultra_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
async def say_lots(num):
1111
for i in range(num):
1212
print(f'This was run by the loop ({i}) :')
13-
await asyncio.sleep(0.2)
13+
await asyncio.sleep(1.0)
1414

1515
# getting the event loop
1616
loop = asyncio.get_event_loop()

source/examples/threading-multiprocessing/threading/integrate_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def worker(*args):
3939
parser.add_argument('a', nargs='?', type=float, default=0.0)
4040
parser.add_argument('b', nargs='?', type=float, default=10.0)
4141
parser.add_argument('N', nargs='?', type=int, default=10**7)
42-
parser.add_argument('thread_count', nargs='?', type=int, default=2)
42+
parser.add_argument('thread_count', nargs='?', type=int, default=4)
4343

4444
args = parser.parse_args()
4545
a = args.a

0 commit comments

Comments
 (0)