Created
September 17, 2018 19:29
-
-
Save bashkirtsevich/ace8add55ac7405b44aa77bfd6db8ec4 to your computer and use it in GitHub Desktop.
Asyncio tasks
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_operation(): | |
| await asyncio.sleep(1) | |
| return 'Future is done!' | |
| def got_result(future): | |
| print(future.result()) | |
| # We have result, so let's stop | |
| loop.stop() | |
| loop = asyncio.get_event_loop() | |
| task = loop.create_task(slow_operation()) | |
| task.add_done_callback(got_result) | |
| # We run forever | |
| loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment