Last active
March 8, 2018 22:10
-
-
Save alairock/a7b4b62f465c4eca6defa5a1bf82b304 to your computer and use it in GitHub Desktop.
Asyncio examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def meow(number): | |
print(f'starting {number}') | |
await asyncio.sleep(1) | |
print(f'stopping {number}') | |
async def run(): | |
# notice how we assign each coroutine to a variable | |
a = meow(1) | |
b = meow(2) | |
c = meow(3) | |
d = meow(4) | |
e = meow(5) | |
# then we can decide which order we wish our coroutines to execute | |
await c | |
await a | |
await e | |
await b | |
await d | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
# RESULTS | |
# starting 3 | |
# stopping 3 | |
# starting 1 | |
# stopping 1 | |
# starting 5 | |
# stopping 5 | |
# starting 2 | |
# stopping 2 | |
# starting 4 | |
# stopping 4 | |
# RUNTIME: 5.2 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def meow(number): | |
await asyncio.sleep(1) | |
return number | |
async def run(): | |
# We create an empty list to store our coroutines in | |
f = [] | |
for x in range(5): | |
f.append(asyncio.ensure_future(meow(x+1))) | |
# this gather will run all items in our list until they are all done. | |
# gather is like wait, but will return the results of the coroutines, instead of the coroutines themselves. | |
x = await asyncio.gather(*f) | |
print(x) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
# RESULTS | |
# [1, 2, 3, 4, 5] | |
# RUNTIME: 1.4 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def new_future(n): | |
print('future', n) | |
await asyncio.sleep(3) | |
print('future done', n) | |
return n | |
async def run(): | |
results = asyncio.ensure_future(new_future(1)) | |
print(results) # <Task> object returned | |
print(await results) # `1` is returned | |
print(results) # <Task> is returned again | |
print(await results) # `1` is returned again | |
results = new_future(2) | |
print(results) # coroutine returned | |
print(await results) # `2` is returned | |
print(results) # coroutine returned again | |
print(await results) # RuntimeError: cannopy reuse aalready awaited coroutine | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def meow(number): | |
print(f'starting {number}') | |
await asyncio.sleep(1) | |
print(f'stopping {number}') | |
async def run(): | |
# We create an empty list to store our coroutines in | |
f = [] | |
for x in range(5): | |
# then we populate our list with 5 couroutines. We use ensure_future which keepts things in order. | |
f.append(asyncio.ensure_future(meow(x+1))) | |
# this will wait for all items to complete. Execution resumes when it's complete | |
await asyncio.wait(f) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
# RESULTS | |
# starting 1 | |
# starting 2 | |
# starting 3 | |
# starting 4 | |
# starting 5 | |
# stopping 1 | |
# stopping 2 | |
# stopping 3 | |
# stopping 4 | |
# stopping 5 | |
# RUNTIME: 1.4 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def meow(number): | |
print(f'starting {number}') | |
await asyncio.sleep(1) | |
print(f'stopping {number}') | |
async def run(): | |
# We create an empty list to store our coroutines in | |
f = [] | |
for x in range(5): | |
# then we populate our list with 5 couroutines. In this example we do not ensure future, | |
# which you will see jumbles our results. | |
f.append(meow(x+1)) | |
await asyncio.wait(f) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
# RESULTS | |
# starting 4 | |
# starting 2 | |
# starting 5 | |
# starting 1 | |
# starting 3 | |
# stopping 4 | |
# stopping 2 | |
# stopping 5 | |
# stopping 1 | |
# stopping 3 | |
# RUNTIME: 1.4 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def meow(number): | |
print(f'starting {number}') | |
await asyncio.sleep(1) | |
print(f'stopping {number}') | |
async def run(): | |
for x in range(5): | |
await meow(x+1) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
# RESULTS | |
# starting 1 | |
# stopping 1 | |
# starting 2 | |
# stopping 2 | |
# starting 3 | |
# stopping 3 | |
# starting 4 | |
# stopping 4 | |
# starting 5 | |
# stopping 5 | |
# RUNTIME: 5.2 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def meow(number): | |
print(f'starting {number}') | |
await asyncio.sleep(1) | |
print(f'stopping {number}') | |
async def run(): | |
f = [] | |
for x in range(5): | |
f.append(meow(x+1)) | |
for x, y in enumerate(f): | |
if x % 2 == 0: | |
await y | |
for x, y in enumerate(f): | |
if x % 2 != 0: | |
await y | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) | |
# RESULTS | |
# starting 1 | |
# stopping 1 | |
# starting 3 | |
# stopping 3 | |
# starting 5 | |
# stopping 5 | |
# starting 2 | |
# stopping 2 | |
# starting 4 | |
# stopping 4 | |
# RUNTIME: 5.2 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment