Created
June 12, 2024 20:05
-
-
Save agronholm/5a8a2773b82369195a308c910566070a to your computer and use it in GitHub Desktop.
Using blocking portal with a loitering event loop thread
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
import asyncio | |
import atexit | |
from contextlib import ExitStack | |
from anyio.from_thread import start_blocking_portal | |
exit_stack = ExitStack() | |
portal = exit_stack.enter_context(start_blocking_portal()) | |
atexit.register(exit_stack.close) | |
portal.call(asyncio.sleep, 1) |
I think I see the problem.
start_blocking_portal()
is using aconcurrent.futures.ThreadPoolExecutor
behind the scenes, and these started using non-daemonic threads in Python 3.9, soatexit
callbacks aren't run, leading to a deadlock. I'll need to make it use plain daemonicThread
instead for consistency.
Nothing is ever as easy as it should be! 😅 I'm glad you could see the root cause as debugging that was definitely outside my experience/expertise!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I see the problem.
start_blocking_portal()
is using aconcurrent.futures.ThreadPoolExecutor
behind the scenes, and these started using non-daemonic threads in Python 3.9, soatexit
callbacks aren't run, leading to a deadlock. I'll need to make it use plain daemonicThread
instead for consistency.