Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions sniffio/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,24 @@ async def generic_sleep(seconds):
if "asyncio" in sys.modules:
import asyncio
try:
current_task = asyncio.current_task # type: ignore[attr-defined]
asyncio.get_running_loop()
return "asyncio"

except AttributeError:
current_task = asyncio.Task.current_task # type: ignore[attr-defined]
try:
if current_task() is not None:
return "asyncio"
try:
current_task = asyncio.current_task # type: ignore[attr-defined]
except AttributeError:
current_task = asyncio.Task.current_task # type: ignore[attr-defined]
try:
if current_task() is not None:
return "asyncio"
except RuntimeError:
pass

except RuntimeError:
pass


# Sniff for curio (for now)
if 'curio' in sys.modules:
from curio.meta import curio_running
Expand Down
24 changes: 24 additions & 0 deletions sniffio/_tests/test_sniffio.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ async def this_is_asyncio():
current_async_library()


def test_in_call_soon_threadsafe():
import asyncio

asynclib = None

def sync_in_loop(completed):
nonlocal asynclib
try:
asynclib = current_async_library()
finally:
completed.set()

async def async_in_loop():
completed = asyncio.Event()
loop.call_soon_threadsafe(sync_in_loop, completed)
await completed.wait()

loop = asyncio.new_event_loop()
loop.run_until_complete(async_in_loop())
loop.close()

assert asynclib == 'asyncio'


# https://github.com/dabeaz/curio/pull/354
@pytest.mark.skipif(
os.name == "nt" and sys.version_info >= (3, 9),
Expand Down