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.

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

Recommended: Loop Factory (New Interface)

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

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.20.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

monitored_ioloop-0.0.20-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file monitored_ioloop-0.0.20.tar.gz.

File metadata

  • Download URL: monitored_ioloop-0.0.20.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for monitored_ioloop-0.0.20.tar.gz
Algorithm Hash digest
SHA256 b2651fa1774ebaa88050db13980ea3a79ffb80e76436248f0d214a55a7f6b358
MD5 344f8b549135802805b6785787b5ba98
BLAKE2b-256 dd73c2ecbb347c01bc6e9f771da55c5307ad382715a61f48a37b5f97e6a2e8fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for monitored_ioloop-0.0.20.tar.gz:

Publisher: deployment.yaml on gnir-work/monitored-ioloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file monitored_ioloop-0.0.20-py3-none-any.whl.

File metadata

File hashes

Hashes for monitored_ioloop-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 e9e3f84b2e61ac24007866c03dc92cd337d8383f9ce8c20eb2c8ce30ac229541
MD5 9ed339895ac2293e35b52f5a1a461436
BLAKE2b-256 ae8f7fb5f6245db9e884036181575fc555899cc7b46f5d0a9cc868e4bb0c9c63

See more details on using hashes here.

Provenance

The following attestation bundles were made for monitored_ioloop-0.0.20-py3-none-any.whl:

Publisher: deployment.yaml on gnir-work/monitored-ioloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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