Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Created December 23, 2021 13:40
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 gdamjan/826a35d5624c12e16b064e83d2d75a2e to your computer and use it in GitHub Desktop.
Save gdamjan/826a35d5624c12e16b064e83d2d75a2e to your computer and use it in GitHub Desktop.
futures are lazy, tasks are eager (in async python)
import asyncio
async def test(s, n):
print(f' {s} started')
await asyncio.sleep(n)
print(f' {s} ends')
async def main():
f = test('future', 1)
t = asyncio.create_task(test('task', 1))
print('lets wait a bit in main')
await asyncio.sleep(3)
print('are we done?')
await asyncio.gather(f, t)
print('we are!')
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment