Why Sniffio is the Python Package You’ve Been Waiting For

Did you know that Python provides an easy-to-use library that helps you deal with asynchronous codes provided in an async library? Yes, it is the sniffio library. Go through this article to learn about the sniffio module in Python.

What is an async library?

If a user wants to write asynchronous code that doesn’t interrupt other applications, async libraries are used. Python provides a good number of libraries that help the user in writing asynchronous code. Some of these are:

  • asyncio (Python)
  • async (JavaScript)
  • async/await (C#)
  • CoTaskMem (C++)

These libraries are written in such a way that they don’t disrupt the main execution thread.

Advantages of async libraries

These libraries have proven to be advantageous due to a couple of reasons. Some of the benefits these libraries hold are as follows:

  • They work well with concurrent tasks, handling.
  • They can handle errors related to thread execution
  • They can easily perform asynchronous I/O
  • Certain primitives called Synchronization primitives are present in them

Some asynchronous operations

Such libraries are successful in performing various operations that involve asynchronous input. These operations are:

  • Handling HTTP requests
  • Operations on Files
  • Queries related to the database
  • Working on data stream inferences

About sniffio

It lets a user know the async library he/she is using. A project called the Trio Project maintains this library. With this, this less code package aims to notify the user of the async package in use.

Syntax of sniffio

If only one Python version exists, go for #1. In the case of Python3, go for the second code. In case the pip doesn’t work, go for #3. Users who have Linux can use the #4 command to install sniffio and work on permissions, too, while those users who have Linux with apt should definitely go for the #5 command.

Besides this, If you have Windows with the py alias, use the #6 command. Use #7 in case of conda and #8 command when you are working on Jupyter Notebook. Lastly, Ubuntu users should prefer the #9 command.

#1
pip install sniffio

#2
pip3 install sniffio

#3
python -m pip install sniffio
python3 -m pip install sniffio

#4
sudo pip3 install sniffio
pip3 install sniffio --user

#5
sudo apt install sniffio

#6
py -m pip install sniffio

#7
conda install -c anaconda sniffio

#8
!pip install sniffio
!pip3 install sniffio

#9
sudo apt install sniffio

Libraries involved

A photo setting of a developer's workspace, highlighting the use of "sniffio" in Python.

The sniffio module includes the following libraries:

  • curio
  • trio
  • asyncio
  • Trio-asyncio

To enable proper working with sniffio, the Trio library requires a Trio 0.6+ version. On the other hand, Trio-asyncio requires a v0.8.2+ version. Thus, you need to assess these versions first before using sniffio to check the async library, which is currently functional.

Apart from this, the sniffio library does string matching, so it is equally important to know which string it will match. Let’s have a look at the strings which you need to specify at the time matching :

  • “curio”
  • “trio”
  • “asyncio”
  • “trio” or “asyncio”, based on the current mode

In case the sniffio package fails to identify the async library or the code has synchronous texts, then you will get an AsyncLibraryNotFoundError error.

Working

The working of sniffio includes using the current_async_library. This function helps us in knowing which async library the user is currently operating. It prints the name of the async library as per name matching.

from sniffio import current_async_library
import trio
import asyncio

async def print_library():
    library = current_async_library()
    print("This is:", library)

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

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

Reversed dependencies

Multiple libraries require an updated version of the sniffio module. Such modules are directly dependent on the sniffio module. Some of them are:

  • aichatbot
  • accomate
  • anyio
  • asn-nmap
  • ChemScraper
  • docsvault
  • fastapi_tag
  • gpt-readme-reader
  • libmv
  • Optimo-API-Testing
  • promptflow-gui
  • python-core
  • scsims
  • seleniumbase
  • tensorflow-ml
  • trialtracker
  • zentra
  • trio

Sniffio Anaconda

Anaconda supports sniffio. If you want to use sniffio with anaconda, use the following command:

conda install -c conda-forge sniffio

Anaconda holds a large number of benefits like:

  • It supports many Python libraries with which sniffio can be used.
  • It is supported on all major platforms like Windows, macOS, and Linux.

Does Python’s async function run as js’s async function?

Definitely, the functioning of both the async functions is the same. They don’t provide blockage to the main thread but run asynchronously. But some differences exist too.

  • JS async functions are executed in Javascript only. In the case of Python, an async library which is designed specifically for this purpose, is used.
  • Javascript functions can be used without the event loop, but the Python async library is dependent on the event loop.

Does sniffio detect trio?

The given package does pattern matching and checks for the trio library in sys.modules. However, it may not find a correct answer in certain cases like the ones mentioned below:

  • If the code deals with synchronous code, this package may not work properly.
  • If you are using Trio in guest mode, it is very difficult for sniffio to assess the async library. That is because guest mode lets the user use the Trio library along with the async library asynchronously.

The given code will help a user assess whether the Trio async library is present or not. It uses the current async library function to go through the code.

import sniffio

if sniffio.current_async_library() == "trio":
    print("Sniffio can detect Trio!")
else:
    print("Sniffio cannot detect Trio.")

Also, in case the code has several asynchronous libraries, the code given below will help a lot. It tells the user which library he is using. It has an except block so that the code doesn’t terminate in case there is no async library.

def get_current_async_library():
    try:
        return sniffio.current_async_library()
    except AsyncLibraryNotFoundError:
        return None

FAQs

Is Sniffio free to use?

Yes, it is free to use.

How can I get support for this module?

A user can check the Sniffio GitHub repository or post on stack overflow using the Sniffio Stack Overflow tag.

What is sniffio license?

It is a free-to-use license under the MIT License or the Apache License 2.0 to check the asynchronous library.

Conclusion

This article discusses how a user can work with the sniffio package in Python. It depicts how we can install the library in different ways on different software. Also, it explains the syntax and the Libraries that are a part of sniffio. Also, it explains the difference between the async functions of Python and JavaScript.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments