Created
May 14, 2020 22:22
-
-
Save coxley/5879f5ceecfbb4624bee23a6cef47510 to your computer and use it in GitHub Desktop.
This demonstrates why asyncio.wait_for should never be used with loop.run_in_executor. Cancellation doesn't cross that boundary.
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 time | |
import asyncio | |
def blocking(): | |
time.sleep(5) | |
print("hell yeah") | |
async def main(): | |
loop = asyncio.get_event_loop() | |
fut = loop.run_in_executor(None, blocking) | |
try: | |
await asyncio.wait_for(fut, 2) | |
except asyncio.TimeoutError: | |
print("aw man") | |
asyncio.run(main()) | |
# > python buggy_timeout.py | |
# aw man | |
# hell yeah |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dzhaugasharov Make sure to document it very clearly next to the timeout. ;)