Skip to content

Instantly share code, notes, and snippets.

@amitkot
Created January 22, 2018 07:36
Show Gist options
  • Save amitkot/c5dbcd4f77127eb63d75c64c9610dd9d to your computer and use it in GitHub Desktop.
Save amitkot/c5dbcd4f77127eb63d75c64c9610dd9d to your computer and use it in GitHub Desktop.
Python asyncio: Testing if task finished already
import asyncio
async def slow():
print('Started task')
await asyncio.sleep(3)
print('Finished task')
async def do_things(loop):
t = asyncio.Task(slow())
for i in range(10):
print(f'--- task finished? {t.done()}')
await asyncio.sleep(0.5)
print('--- Awaiting task')
await t
def main():
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(do_things(loop))
finally:
loop.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment