No project description provided
Project description
Monitored IO Loop
A production ready monitored IO loop for Python.
No more wondering why your event loop (or random pieces of your code) are suddenly popping up as slow in your monitoring.
Getting started
Installation
pip install monitored_io_loop # For the default event loop
pip install monitored_io_loop[uvloop] # For the the additional support of the uvloop event loop
Usage asyncio
from monitored_ioloop.monitored_asyncio import MonitoredAsyncIOEventLoopPolicy, IoLoopMonitorState
import asyncio
import time
def monitor_callback(ioloop_state: IoLoopMonitorState) -> None:
print(ioloop_state)
async def test_coroutine() -> None:
time.sleep(2)
def main():
asyncio.set_event_loop_policy(MonitoredAsyncIOEventLoopPolicy(monitor_callback))
asyncio.run(test_coroutine())
Usage uvloop
In order to use the uvloop event loop, please make sure to install monitored_io_loop[uvloop]
.
from monitored_ioloop.monitored_uvloop import MonitoredUvloopEventLoopPolicy, IoLoopMonitorState
import asyncio
import time
def monitor_callback(ioloop_state: IoLoopMonitorState) -> None:
print(ioloop_state)
async def test_coroutine() -> None:
time.sleep(2)
def main():
asyncio.set_event_loop_policy(MonitoredUvloopEventLoopPolicy(monitor_callback))
asyncio.run(test_coroutine())
The monitor callback
The monitor callback will be called for every execution that the event loop initiates.
With every call you will receive an IoLoopMonitorState object that contains the following information:
wall_loop_duration
: The time it took to execute the current step of the event loop in wall time.cpu_loop_duration
: The time it took to execute the current step of the event loop in CPU time.
Performance impact
As many of you might be concerned about the performance impact of this library, I have run some benchmarks to measure the performance impact of this library.
After running the following benchmarks I can confidently say that the performance impact is negligible.
Hardware: Apple M1 Pro 2022 32GB RAM
Vanilla asyncio
Monitored asyncio
Vanilla uvloop Monitored uvloop
Roadmap
- Add support for the amount of
Handle
's on the event loop - Add visibility into which
Handle
are making the event loop slower - Add easier integration with popular monitoring tools like Prometheus
- Add easier integration with
uvicorn
- Add an examples folder
Credits
I took a lot of inspiration from the uvloop project with everythin regarding the user interface of swapping the IO loop.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for monitored_ioloop-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a615547b4aee31c6e319a793e6611168da7fcecbee4fa94639b6a9978f0806a |
|
MD5 | 7f951c7368bbd828fac78badc175cc6d |
|
BLAKE2b-256 | 5842728658a30e66b63fe4f6625c5a43fe445fbb5c4498e8fa549742c58e63d6 |