sniffio - Top-level package for sniffio.
| Use Case | Command | Description |
|---|---|---|
| 🔍 Detect current async library | current_async_library() | Returns a string like "trio", "asyncio", or "curio". |
| 🛡️ Handle unknown/unsupported context | try: ... except AsyncLibraryNotFoundError | Catches error when called from synchronous code or unrecognised library. |
_impl_tests (package)_versionMethod resolution order:
AsyncLibraryNotFoundError
builtins.RuntimeError
builtins.Exception
builtins.BaseException
builtins.object
__weakref__ — list of weak references to the object (if defined)__init__(self, /, *args, **kwargs) — Initialize self. See help(type(self)) for accurate signature.__new__(*args, **kwargs) from builtins.type — Create and return a new object. See help(type) for accurate signature.__delattr__(self, name, /) — Implement delattr(self, name).__getattribute__(self, name, /) — Return getattr(self, name).__reduce__(...) — Helper for pickle.__repr__(self, /) — Return repr(self).__setattr__(self, name, value, /) — Implement setattr(self, name, value).__setstate__(...)__str__(self, /) — Return str(self).with_traceback(...) — Exception.with_traceback(tb) — set self.__traceback__ to tb and return self.__cause__ — exception cause__context__ — exception context__dict____suppress_context____traceback__argsDetect which async library is currently running.
The following libraries are currently supported:
| Library | Requires | Magic string |
|---|---|---|
| 🎵 Trio | Trio v0.6+ | "trio" |
| 🔮 Curio | - | "curio" |
| ⚡ asyncio | "asyncio" | |
| 🎵⚡ Trio-asyncio | v0.8.2+ | "trio" or "asyncio", depending on current mode |
Returns: A string like "trio".
Raises: AsyncLibraryNotFoundError — if called from synchronous context, or if the current async library was not recognized.
Examples:
from sniffio import current_async_library
async def generic_sleep(seconds):
library = current_async_library()
if library == "trio":
import trio
await trio.sleep(seconds)
elif library == "asyncio":
import asyncio
await asyncio.sleep(seconds)
# ... and so on ...
else:
raise RuntimeError(f"Unsupported library {library!r}")
__all__ = ['current_async_library', 'AsyncLibraryNotFoundError', 'current_async_library_cvar']current_async_library_cvar = <ContextVar name='current_async_library_cvar'>thread_local = <sniffio._impl._ThreadLocal object>1.3.1
/home/chedong/.local/lib/python3.10/site-packages/sniffio/__init__.py
Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 16:06 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format