Skip to main content

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.

GitHub Actions Workflow Status PyPI - Version PyPI - Python Version

Getting started

Installation

pip install monitored_ioloop  # For the default event loop
pip install monitored_ioloop[uvloop]  # For the the additional support of the uvloop event loop

Demo

📺 Play with the demo in sandbox

Usage

The loop factory approach is the recommended way to use monitored event loops. This approach is preferred by Python's asyncio documentation as it provides more flexibility and allows running asyncio without the policy system.

Asyncio event loop

from monitored_ioloop.monitored_asyncio import monitored_asyncio_loop_factory
from monitored_ioloop.monitoring import 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():
    loop_factory = monitored_asyncio_loop_factory(monitor_callback)
    asyncio.run(test_coroutine(), loop_factory=loop_factory)


if __name__ == "__main__":
    main()

Uvloop event loop

In order to use the uvloop event loop, please make sure to install monitored_ioloop[uvloop].

from monitored_ioloop.monitored_uvloop import monitored_uvloop_loop_factory
from monitored_ioloop.monitoring import 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():
    loop_factory = monitored_uvloop_loop_factory(monitor_callback)
    asyncio.run(test_coroutine(), loop_factory=loop_factory)


if __name__ == "__main__":
    main()

Legacy: Event Loop Policy Interface

For backward compatibility, you can still use the event loop policy interface:

from monitored_ioloop.monitored_asyncio import MonitoredAsyncIOEventLoopPolicy
from monitored_ioloop.monitoring import 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())

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:

  • callback_wall_time: Wall executing time of the callback.
  • loop_handles_count: The amount of handles (think about them as tasks) that the IO loop is currently handling.
  • loop_lag: The amount of time it took from the moment the task was added to the loop until it was executed.
  • callback_pretty_name: The pretty name of the callback that was executed
    Please Note: This is a best effort, the name of the callback may still be of little help, depending on the specific callback implementation.

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.
In summary the performance impact is negligible for most use cases.
You can find the more detailed information in the following README.md.

Usage examples

You can find examples projects showing potential use cases in the examples folder.
Currently there is only the fastapi with prometheus exporter example but more will be added in the future.

Roadmap

  • Add support for the amount of Handle's on the event loop
  • Add an examples folder
  • Add loop lag metric (Inspired from nodejs loop monitoring)
  • Add visibility into which Handle are making the event loop slower
  • Migrate to new loop factory interface
  • Add easier integration with uvicorn
  • Add easier integration with popular monitoring tools like Prometheus

Credits

Metadata

Release files for monitored-ioloop 0.0.23

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monitored-ioloop 0.0.23
File Size Uploaded Provenance Actions
monitored_ioloop-0.0.23.tar.gz 8.8 kB With provenance Details for monitored_ioloop-0.0.23.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for monitored-ioloop 0.0.23
File Interpreter ABI Platform Actions
monitored_ioloop-0.0.23-py3-none-any.whl Python 3 none any Details for monitored_ioloop-0.0.23-py3-none-any.whl

Total release size: 19.9 kB

Release files / monitored_ioloop-0.0.23.tar.gz

Download URL monitored_ioloop-0.0.23.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2b46654c9ae9070ee074b824232c6829a9a164d42418e53d58117943e0153c38
BLAKE2b-256 checksum
How to use checksums
b0cd5e93960857d382e3d82259d6a764b6c46fe247268be739a88b9a22604ee6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 11, 2026.

Transparency log

Release files / monitored_ioloop-0.0.23-py3-none-any.whl

Download URL monitored_ioloop-0.0.23-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
776d264cf932769a4fc30e9887f81943a85838d491d7d232bb4521c81321c8a9
BLAKE2b-256 checksum
How to use checksums
b0224cdbc379a026fec9151083db324e8186160f232e36e2edcd01fd8ee4e06b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 11, 2026.

Transparency log

Security for monitored-ioloop 0.0.23

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for monitored_ioloop-0.0.23.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page