Skip to content

Instantly share code, notes, and snippets.

@AndreLouisCaron
Last active February 13, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreLouisCaron/db2965aae095f5c85dd5 to your computer and use it in GitHub Desktop.
Save AndreLouisCaron/db2965aae095f5c85dd5 to your computer and use it in GitHub Desktop.
asyncio await inconsistency
# This will print nothing because the set contains tasks, not f1 and f2.
import asyncio
async def foo():
return 'foo'
async def bar():
f1 = foo();
f2 = foo();
done, pending = await asyncio.wait(
[f1, f2], return_when=asyncio.ALL_COMPLETED,
)
assert len(pending) == 0
assert len(done) == 2
if f1 in done:
print('1:', f1.result())
if f2 in done:
print('2:', f2.result())
print('---')
asyncio.get_event_loop().run_until_complete(bar())
import asyncio
async def foo():
return 'foo'
async def bar():
f = foo()
print('1:', await f)
print('2:', await f)
asyncio.get_event_loop().run_until_complete(bar())
import asyncio
async def foo():
return 'foo'
async def bar():
f = asyncio.ensure_future(foo())
print('1:', await f)
print('2:', await f)
asyncio.get_event_loop().run_until_complete(bar())
import asyncio
async def foo():
return 'foo'
async def bar():
f = asyncio.get_event_loop().create_task(foo())
print('1:', await f)
print('2:', await f)
asyncio.get_event_loop().run_until_complete(bar())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment