We can use third-party libraries to assist with common asyncio software development tasks.
This includes tasks such as logging in our asyncio programs, debugging, unit testing, linting, and profiling asyncio programs.
Many of these capabilities are provided in the Python standard library and can support asyncio programs, perhaps after some configuration. Nevertheless, we may be able to develop simpler asyncio programs in less time using helpful third-party libraries to assist with these common software development tasks.
In this tutorial, you will discover popular libraries for software development tasks in asyncio.
Let’s get started.
Asyncio Libraries For Software Engineering Tasks
Asyncio programs are different from regular Python programs.
They execute within a runtime, called the asyncio event loop, and involve the collaboration between coroutines, rather than the competition between tasks.
Nevertheless, we must still use software engineering practices when developing asyncio programs, and this typically involves making use of third-party libraries.
ASyncio libraries for common software engineering tasks can be divided into 5 main groups, they are:
- Asyncio Logging
- Asyncio Debugging
- Asyncio Unit Testing
- Asyncio Static Analysis / Linting
- Asyncio Profiling
Let’s take a closer look at each area.
It’s really hard to track down good libraries on these topics. For more on how to find asyncio libraries, see the tutorial:
Did I miss a library?
Let me know in the comments below.
Run loops using all CPUs, download your FREE book to learn how.
Asyncio Logging Libraries
Logging is the systematic recording of events, actions, or data within a software system to aid in understanding its behavior, diagnosing issues, and monitoring performance.
It involves strategically placed commands that capture information during the execution of a program, such as status messages, errors, warnings, or custom messages. Logging serves multiple purposes, including providing a history of program execution, facilitating troubleshooting by recording significant events, assisting in identifying bugs or unexpected behavior and enabling post-execution analysis for performance optimization or security auditing.
Effective logging practices involve managing log levels, formatting log messages, and storing logs in a structured and accessible manner to support efficient analysis and maintenance of software systems.
Python provides the logging module in the standard library, which covers most cases.
Unfortunately, the default logger is blocking, meaning it is not appropriate for use in most asyncio applications.
Network logging can block the event loop. It is recommended to use a separate thread for handling logs or use non-blocking IO.— Developing with asyncio
We can configure the logging infrastructure to use non-blocking logging.
Nevertheless, there are libraries that offer support for logging in asyncio programs.
Below is a list of some of the more popular asyncio logging libraries.
- aiologger: Asynchronous logging for Python and asyncio
- aiologstash: asyncio logging handler for logstash
- aiolog: Asynchronous handlers for standard python logging library
- contextlogger: A logging boilerplate enhanced by the use of contextvars
- noblklog: Non-blocking asyncio handlers for Python logging.
Additionally, the following libraries offer support for non-blocking logging (as one of many features):
- aiomisc: miscellaneous utils for asyncio
Asyncio Debugging Libraries
Debugging is the systematic process of identifying, isolating, and resolving issues or bugs within software code.
It involves a comprehensive analysis of code behavior to pinpoint errors that cause unexpected or undesired outcomes.
We can utilize various tools and techniques, like stepping through code, inspecting variables, and setting breakpoints, to trace the flow of execution and identify the root cause of issues. Debugging aims to fix software defects, enhance code quality, and ensure the program operates correctly by rectifying logical errors, addressing unexpected behaviors, and improving overall code performance.
Debugging is typically an activity performed within the IDE, although we can instrument the asyncio event loop to report debug information.
For example, the asyncio module in the standard library offers debug support.
Specifically, the event loop can be configured to run in debug mode, which offers more warnings and logging. Helpfully, it can also report tasks that block the event loop for too long.
We can also configure the Python warning system to report more warnings while running the program.
Below is a list of some of the more popular asyncio debugging libraries.
- aiodebug: A tiny library for monitoring and testing asyncio programs
- aiohttp_debugtoolbar: for debugtoolbar support for aiohttp
- asyncio-inspector: Deadly simple statistics tracker for Python’s asyncio library
- aiomanhole: Manhole for accessing asyncio applications
Free Python Asyncio Course
Download your FREE Asyncio PDF cheat sheet and get BONUS access to my free 7-day crash course on the Asyncio API.
Discover how to use the Python asyncio module including how to define, create, and run new coroutines and how to use non-blocking I/O.
Asyncio Unit Testing Libraries
Unit testing is a software testing method where individual components or units of code are tested in isolation to ensure they perform as intended.
These tests target specific functions, methods, or classes, checking their behavior against expected outcomes.
Through unit tests, developers validate the correctness of small, independent units of code, aiming to identify bugs early in the development process. Unit testing helps ensure that each piece of code functions as expected, facilitating code maintainability, enabling faster debugging, and providing confidence in the overall system’s reliability and stability.
Python provides the unittest module in the standard library for unit testing, and it supports unit testing asyncio code via the IsolatedAsyncioTestCase class. Mocking is supported by the AsyncMock class.
Given that a large fraction of asyncio development is web development-focused, many of the libraries for assisting asyncio unit testing support mocking web requests.
Below is a list of some of the more popular asyncio unit testing helpers and libraries.
- aiounittest: Test python asyncio-based code with ease.
- asynctest: Enhance the standard unittest package with features for testing asyncio libraries
- pytest-asyncio: Asyncio support for pytest
- aioloop-proxy: A proxy for asyncio.AbstractEventLoop for testing purposes
Below is a list of test helpers for playing with time:
- looptime: Time dilation & contraction in asyncio event loops (in tests)
- aiofastforward: Fast-forward time in asyncio
Below is a list of some of the more popular asyncio unit testing mocking libraries.
- aiomock: A version of pythons unittest.Mock with async methods
- aresponses: Asyncio http mocking. Similar to the responses library used for ‘requests’
- aioresponses: Aioresponses is a helper for mock/fake web requests in python aiohttp package.
- python-mocket: https://github.com/mindflayer/python-mocket
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Asyncio Static Analysis Libraries
Static analysis and linting are software development practices used to analyze source code for potential errors, bugs, stylistic inconsistencies, or security vulnerabilities without executing the code.
Static analysis involves examining the codebase without actually running the program, employing various techniques to detect issues like syntax errors, type mismatches, unreachable code, or common programming mistakes.
Linting, a subset of static analysis, involves employing specialized tools or linters that automatically check code against a predefined set of coding standards, style guidelines, or best practices specific to a programming language.
These tools identify deviations from the established conventions, ensuring consistent and maintainable code, reducing potential bugs, and enhancing code quality through automated checks during the development process.
Below is a list of some of the more popular asyncio plugins for Python linting libraries.
- pylint-blocking-calls: Pylint Blocking Calls Checker
- flake8-async: A flake8 plugin that checks bad async / asyncio practices
Loving The Tutorials?
Why not take the next step? Get the book.
Asyncio Profiling Libraries
Profiling is a software performance analysis technique used to examine and measure the execution behavior of a program, identifying areas that consume the most resources, such as CPU time, memory, or disk usage.
It involves collecting data on various aspects of program execution, like function call durations, memory allocation, or I/O operations.
Profiling aims to uncover bottlenecks or inefficiencies within the code, allowing developers to optimize critical sections, enhance overall performance, and identify areas for improvement.
This process provides insights into how the program operates during execution, helping developers make informed decisions about optimizing specific code segments to achieve better efficiency and responsiveness.
Python ships with a comprehensive profiler via the profiler module in the standard library.
This profiler can be used directly with asyncio programs.
For example, you can see an example in the tutorial:
Most third-party Python profilers support asyncio directly.
Below is a list of some of the more popular asyncio profiling libraries.
- yappi: Yet Another Python Profiler, but this time multithreading, asyncio and gevent aware.
- fullprofiler: Profile asyncio code in addition to normal sync code
- pyinstrument: Call stack profiler for Python. Shows you why your code is slow!
- capara: Python profiler written in Rust for Contextual profiling.
Additionally, profiler libraries for regular Python can also support asyncio programs.
Some popular examples include:
- line_profiler: Line-by-line profiling for Python
You can see an example of how to use line_profiler for asyncio programs in the tutorial:
This section provides additional resources that you may find helpful.
Python Asyncio Books
- Python Asyncio Mastery, Jason Brownlee (my book!)
- Python Asyncio Jump-Start, Jason Brownlee.
- Python Asyncio Interview Questions, Jason Brownlee.
- Asyncio Module API Cheat Sheet
I also recommend the following books:
- Python Concurrency with asyncio, Matthew Fowler, 2022.
- Using Asyncio in Python, Caleb Hattingh, 2020.
- asyncio Recipes, Mohamed Mustapha Tahrioui, 2019.
- asyncio — Asynchronous I/O
- Asyncio Coroutines and Tasks
- Asyncio Streams
- Asyncio Subprocesses
- Asyncio Queues
- Asyncio Synchronization Primitives
You now know about popular libraries for software development tasks in asyncio.
Did I make a mistake? See a typo?
I’m a simple humble human. Correct me, please!
Do you have any additional tips?
I’d love to hear about them!
Did I miss a library?
Let me know in the comments below.