Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

monitored-ioloop

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

monitored-ioloop

  • 0.0.11
  • PyPI
  • Socket score

Maintainers
1

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

Asyncio event loop
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())
Uvloop event loop

In order to use the uvloop event loop, please make sure to install monitored_ioloop[uvloop].
The usage is the same as the asyncio event loop, but with monitored_ioloop.monitored_uvloop.MonitoredUvloopEventLoopPolicy instead of the monitored_ioloop.monitored_asyncio.MonitoredAsyncIOEventLoopPolicy.

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
  • Add easier integration with uvicorn
  • Add easier integration with popular monitoring tools like Prometheus

Credits

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc