Skip to content

Instantly share code, notes, and snippets.

@coxley
Created May 14, 2020 22:22
Show Gist options
  • Save coxley/5879f5ceecfbb4624bee23a6cef47510 to your computer and use it in GitHub Desktop.
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.
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
@dzhaugasharov
Copy link

I have got the same issue. After a timeout, the loop is still running. But it is ok for my case.

@coxley
Copy link
Author

coxley commented Feb 12, 2022

@dzhaugasharov Make sure to document it very clearly next to the timeout. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment