Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

aiomonitor is module that adds monitor and python REPL capabilities for asyncio application

License

NotificationsYou must be signed in to change notification settings

aio-libs/aiomonitor

GitHub Actions status for the main branchcodecov.io status for the main branchLatest PyPI package versionDownloads countDocumentation Status

aiomonitor is a module that adds monitor and cli capabilitiesforasyncio applications. Idea and code were borrowed fromcurio project.Task monitor that runs concurrently to theasyncio loop (or fast drop-inreplacementuvloop) in a separate thread as result monitor will work even ifthe event loop is blocked for some reason.

This library provides a python console usingaioconsole module. It is possibleto execute asynchronous commands inside your running application. Extensiblewith you own commands, in the style of the standard library'scmd module

An example to run the aiomonitor shell

Installation

Installation process is simple, just:

$ pip install aiomonitor

Example

Monitor has context manager interface:

importaiomonitorasyncdefmain():loop=asyncio.get_running_loop()run_forever=loop.create_future()withaiomonitor.start_monitor(loop):awaitrun_forevertry:asyncio.run(main())exceptKeyboardInterrupt:pass

Now from separate terminal it is possible to connect to the application:

$ telnet localhost 20101

or the included python client:

$ python -m aiomonitor.cli

Tutorial

Let's create a simpleaiohttp application, and see howaiomonitor canbe integrated with it.

importasyncioimportaiomonitorfromaiohttpimportweb# Simple handler that returns response after 100sasyncdefsimple(request):print('Start sleeping')awaitasyncio.sleep(100)returnweb.Response(text="Simple answer")loop=asyncio.get_event_loop()# create application and register routeapp=web.Application()app.router.add_get('/simple',simple)# it is possible to pass a dictionary with local variables# to the python console environmenthost,port="localhost",8090locals_= {"port":port,"host":host}# init monitor just before run_appwithaiomonitor.start_monitor(loop=loop,locals=locals_):# run application with built-in aiohttp run_app functionweb.run_app(app,port=port,host=host,loop=loop)

Let's save this code in filesimple_srv.py, so we can run it with the following command:

$ python simple_srv.py======== Running on http://localhost:8090 ========(Press CTRL+C to quit)

And now one can connect to a running application from a separate terminal, withthetelnet command, andaiomonitor will immediately respond with prompt:

$ telnet localhost 20101Asyncio Monitor: 1 tasks runningType help for commandsmonitor >>>

Now you can type commands, for instance,help:

monitor >>> helpUsage: help [OPTIONS] COMMAND [ARGS]...  To see the usage of each command, run them with "--help" option.Commands:  cancel                  Cancel an indicated task  console                 Switch to async Python REPL  exit (q,quit)           Leave the monitor client session  help (?,h)              Show the list of commands  ps (p)                  Show task table  ps-terminated (pst,pt)  List recently terminated/cancelled tasks  signal                  Send a Unix signal  stacktrace (st,stack)   Print a stack trace from the event loop thread  where (w)               Show stack frames and the task creation chain of a task  where-terminated (wt)   Show stack frames and the termination/cancellation chain of a task

aiomonitor also supports async python console inside a running event loopso you can explore the state of your application:

monitor >>> consolePython 3.10.7 (main, Sep  9 2022, 12:31:20) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwinType "help", "copyright", "credits" or "license" for more information.---This console is running in an asyncio event loop.It allows you to wait for coroutines using the 'await' syntax.Try: await asyncio.sleep(1, result=3)--->>> await asyncio.sleep(1, result=3)3>>>

To leave the console typeexit() or press Ctrl+D:

>>> exit()✓ The console session is closed.monitor >>>

Extension

Additional console variables

You may add more variables that can be directly referenced in theconsole command.Referthe console-variables example code

Custom console commands

aiomonitor is very easy to extend with your own console commands.Referthe extension example code

Requirements


[8]ページ先頭

©2009-2025 Movatter.jp