Created
October 19, 2020 08:43
-
-
Save bitnom/4ac38bf3faa84b57591b960729091d05 to your computer and use it in GitHub Desktop.
This file contains 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
def blocking_io(): | |
print(f"start blocking_io at {time.strftime('%X')}") | |
# Note that time.sleep() can be replaced with any blocking | |
# IO-bound operation, such as file operations. | |
time.sleep(1) | |
print(f"blocking_io complete at {time.strftime('%X')}") | |
async def main(): | |
print(f"started main at {time.strftime('%X')}") | |
await asyncio.gather( | |
asyncio.to_thread(blocking_io), | |
asyncio.sleep(1)) | |
print(f"finished main at {time.strftime('%X')}") | |
asyncio.run(main()) | |
# Expected output: | |
# | |
# started main at 19:50:53 | |
# start blocking_io at 19:50:53 | |
# blocking_io complete at 19:50:54 | |
# finished main at 19:50:54 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment