Skip to main content

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 Vanilla asyncio Monitored asyncio Monitored asyncio

Vanilla uvloop Vanilla uvloop Monitored 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

monitored_ioloop-0.0.2.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

monitored_ioloop-0.0.2-py3-none-any.whl (6.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page