Created
January 22, 2018 07:36
-
-
Save amitkot/c5dbcd4f77127eb63d75c64c9610dd9d to your computer and use it in GitHub Desktop.
Python asyncio: Testing if task finished already
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 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