cover anyio’s NoEventLoopError without altering existing behavior#145
Open
gotsysdba wants to merge 2 commits intooracle:mainfrom
Open
cover anyio’s NoEventLoopError without altering existing behavior#145gotsysdba wants to merge 2 commits intooracle:mainfrom
gotsysdba wants to merge 2 commits intooracle:mainfrom
Conversation
Member
|
@gotsysdba , I tried your solution using
But I was still getting an error Using the following works for me however: def _is_anyio_worker_thread() -> bool:
try:
# AnyIO "claims" worker threads spawned by `to_thread.run_sync()` by storing
# a current_token on this thread-local. Plain synchronous threads do not have
# it, and AnyIO's own `from_thread.run*()` helpers use the same signal, so
# this is more robust than checking thread names or version-specific
# "no event loop" exceptions.
from_thread.threadlocals.current_token # type: ignore[attr-defined]
except AttributeError:
return False
else:
return True
def get_execution_context() -> AsyncContext:
"""
Return one of:
- 'sync' → plain synchronous context (no loop, no worker thread)
- 'sync_worker' → synchronous worker thread (spawned by to_thread.run_sync)
- 'async' → running inside the event loop
"""
try:
current_async_library()
return AsyncContext.ASYNC
except AsyncLibraryNotFoundError:
if _is_anyio_worker_thread():
# for anyio workers, we can use specific methods to
# handle back asynchronous code to the main loop
return AsyncContext.SYNC_WORKER
# otherwise, consider it as a synchronous thread
return AsyncContext.SYNC |
Member
Author
|
Thanks @paul-cayet, yours is a much cleaner, more reliable detection though I couldn't find any docs on I must have tested mine back on MacOS (where it wasn't a problem) and/or with my monkeypatch in place.. too many terminals :(). I've tested this suggestion on both MacOS and OL8. TYVM for the review. |
Contributor
|
Internal regression succeeded 🍏: Build ID #394 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #144
Note once the minimal version of anyio ≥ 4.11, the backward compatibility code can be removed:
Replace with: