Asyncio Application Monitor and REPL

0.7.1 · active · verified Mon Apr 13

aiomonitor is a Python library that enhances asyncio applications by providing interactive monitoring and a Python REPL (Read-Eval-Print Loop). It allows developers to inspect the state of running asyncio event loops, tasks, and execute arbitrary asynchronous Python code within the application's context via a telnet interface. Since version 0.6.0, it also includes a web-based UI for task inspection and cancellation. The current version is 0.7.1, with a moderate release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `aiomonitor` into an asyncio application using its context manager interface. It starts a background worker task and keeps the main loop running. Once executed, you can connect via `telnet localhost 20101` or access the web UI at `http://localhost:20102` to inspect and interact with the running application.

import asyncio
import aiomonitor

async def worker():
    print("Worker started...")
    try:
        await asyncio.sleep(100) # Simulate a long-running task
    except asyncio.CancelledError:
        print("Worker cancelled!")
    finally:
        print("Worker finished.")

async def main():
    loop = asyncio.get_running_loop()
    # start_monitor automatically starts telnet (20101) and web (20102) interfaces
    with aiomonitor.start_monitor(loop=loop):
        print("aiomonitor started. Connect via telnet localhost 20101 or browser http://localhost:20102")
        task = loop.create_task(worker())
        # Keep the main loop running indefinitely
        await asyncio.Event().wait()
        # The task might be cancelled from the monitor, so await it for cleanup
        await task

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("Application gracefully stopped.")

view raw JSON →