sniffio

raw JSON →
1.3.1 verified Tue May 12 auth: no python install: verified quickstart: stale

A Python library that detects which asynchronous I/O library (e.g., Trio, asyncio) is currently running in your code. Current version: 1.3.1, released on February 25, 2024. Maintained by the Trio project, it has a stable release cadence with updates approximately every 1-2 years.

pip install sniffio
error RuntimeError: This function is only available from inside a Trio or asyncio task.
cause You are attempting to call `sniffio.current_async_library()` (or a similar sniffio function) from a synchronous context, outside of an active Trio or asyncio event loop.
fix
Ensure that calls to sniffio.current_async_library() are made within an async function that is itself executed by an asynchronous runner like asyncio.run() or trio.run().
import sniffio
import asyncio

async def my_async_function():
    print(f"Current async library: {sniffio.current_async_library()}")

asyncio.run(my_async_function())
error ModuleNotFoundError: No module named 'sniffio'
cause The `sniffio` package has not been installed in your current Python environment.
fix
Install the sniffio package using pip.
pip install sniffio
error AttributeError: module 'sniffio' has no attribute 'async_library'
cause You are attempting to access a non-existent attribute or made a typo when trying to call the correct function to detect the current async library.
fix
Use the correct function name sniffio.current_async_library() to retrieve information about the running async library.
import sniffio
import asyncio

async def get_library_info():
    lib = sniffio.current_async_library()
    if lib:
        print(f"Detected: {lib.sniffio_name}")
    else:
        print("No async library detected.")

asyncio.run(get_library_info())
error AttributeError: 'NoneType' object has no attribute 'sniffio_name'
cause This error occurs when `sniffio.current_async_library()` returns `None` (indicating no async event loop is running), and you then attempt to access an attribute like `sniffio_name` directly on that `None` value without checking if a library was actually detected.
fix
Always check if the return value of sniffio.current_async_library() is not None before attempting to access its attributes.
import sniffio
import asyncio

async def check_and_use_library():
    detected_lib = sniffio.current_async_library()
    if detected_lib:
        print(f"Running with: {detected_lib.sniffio_name}")
    else:
        print("No async library is currently active.")

asyncio.run(check_and_use_library())
breaking Dropped support for Python 3.5 and 3.6 in version 1.3.0
fix Upgrade to Python 3.7 or later.
deprecated Explicit version specifications in USES=python for '3.x+' are deprecated
fix Remove explicit version specifications in USES=python.
breaking ModuleNotFoundError: No module named 'trio'. The 'trio' package is not installed.
fix Install the 'trio' package using pip: `pip install trio`.
breaking Module 'trio' not found. Ensure the 'trio' package is installed in the environment.
fix Install the 'trio' package using pip (e.g., `pip install trio`) or ensure it's included in your project's dependencies.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.01s 17.8M
3.10 slim (glibc) - - 0.01s 18M
3.11 alpine (musl) - - 0.02s 19.7M
3.11 slim (glibc) - - 0.02s 20M
3.12 alpine (musl) - - 0.02s 11.6M
3.12 slim (glibc) - - 0.02s 12M
3.13 alpine (musl) - - 0.01s 11.2M
3.13 slim (glibc) - - 0.01s 12M
3.9 alpine (musl) - - 0.01s 17.3M
3.9 slim (glibc) - - 0.01s 18M

A simple script demonstrating how to use sniffio to detect the current async library and run a coroutine accordingly.

import sniffio
import trio
import asyncio

async def print_library():
    library = sniffio.current_async_library()
    print(f'This is {library}')

# Prints 'This is trio'
trio.run(print_library)

# Prints 'This is asyncio'
asyncio.run(print_library())