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 sniffioVerified import paths — ran on the pinned version, not inferred.
A simple script demonstrating how to use sniffio to detect the current async library and run a coroutine accordingly.
Upgrade to Python 3.7 or later.
Remove explicit version specifications in USES=python.
Install the 'trio' package using pip: `pip install trio`.
Install the 'trio' package using pip (e.g., `pip install trio`) or ensure it's included in your project's dependencies.
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()`.
```python
import sniffio
import asyncio
async def my_async_function():
print(f"Current async library: {sniffio.current_async_library()}")
asyncio.run(my_async_function())
```Install the `sniffio` package using pip. ```bash pip install sniffio ```
Use the correct function name `sniffio.current_async_library()` to retrieve information about the running async library.
```python
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())
```Always check if the return value of `sniffio.current_async_library()` is not `None` before attempting to access its attributes.
```python
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())
```