Skip to content

Instantly share code, notes, and snippets.

@yeraydiazdiaz
Created February 21, 2017 08:51
Show Gist options
  • Save yeraydiazdiaz/ae49c999ef6082d84c17c9837eedf593 to your computer and use it in GitHub Desktop.
Save yeraydiazdiaz/ae49c999ef6082d84c17c9837eedf593 to your computer and use it in GitHub Desktop.
1b-cooperatively-scheduled-asyncio-await.py
import time
import asyncio
start = time.time()
def tic():
return 'at %1.1f seconds' % (time.time() - start)
async def gr1():
# Busy waits for a second, but we don't want to stick around...
print('gr1 started work: {}'.format(tic()))
await asyncio.sleep(2)
print('gr1 ended work: {}'.format(tic()))
async def gr2():
# Busy waits for a second, but we don't want to stick around...
print('gr2 started work: {}'.format(tic()))
await asyncio.sleep(2)
print('gr2 Ended work: {}'.format(tic()))
async def gr3():
print("Let's do some stuff while the coroutines are blocked, {}".format(tic()))
await asyncio.sleep(1)
print("Done!")
ioloop = asyncio.get_event_loop()
tasks = [
ioloop.create_task(gr1()),
ioloop.create_task(gr2()),
ioloop.create_task(gr3())
]
ioloop.run_until_complete(asyncio.wait(tasks))
ioloop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment