跳转到主要内容

aiomonitor-ng为asyncio应用添加监控和Python REPL功能

项目描述

aiomonitor-ng

aiomonitor-ngaiomonitor的一个(临时)分支,支持Python 3.10+,并增加了可用性和可调试性的改进。

aiomonitor是一个模块,为asyncio应用程序添加了监控和cli功能。思想和代码借鉴自curio项目。任务监控器与asyncio循环(或快速替换uvloop)并行运行,在单独的线程中,因此即使事件循环由于某些原因而阻塞,监控器也能正常工作。

此库提供使用aioconsole模块的Python控制台。可以在运行中的应用程序中执行异步命令。可以扩展您自己的命令,类似于标准库中的cmd模块。

https://raw.githubusercontent.com/achimnol/aiomonitor-ng/master/docs/screenshot-ps-where-example.png

安装

安装过程很简单,只需

$ pip install aiomonitor-ng

示例

监控器具有上下文管理器接口

import aiomonitor

async def main():
    loop = asyncio.get_running_loop()
    run_forever = loop.create_future()
    with aiomonitor.start_monitor(loop):
        await run_forever

try:
    asyncio.run(main())
except KeyboardInterrupt:
    pass

现在可以从单独的终端连接到应用程序

$ telnet localhost 50101

或包含的Python客户端

$ python -m aiomonitor.cli

教程

让我们创建一个简单的aiohttp应用程序,并看看如何将aiomonitor集成到其中。

import asyncio

import aiomonitor
from aiohttp import web

# Simple handler that returns response after 100s
async def simple(request):
    loop = request.app.loop

    print('Start sleeping')
    await asyncio.sleep(100, loop=loop)
    return web.Response(text="Simple answer")

loop = asyncio.get_event_loop()
# create application and register route
app = web.Application(loop=loop)
app.router.add_get('/simple', simple)

# it is possible to pass a dictionary with local variables
# to the python console environment
host, port = "localhost", 8090
locals_ = {"port": port, "host": host}
# init monitor just before run_app
with aiomonitor.start_monitor(loop=loop, locals=locals_):
    # run application with built-in aiohttp run_app function
    web.run_app(app, port=port, host=host)

让我们将此代码保存到文件simple_srv.py中,然后可以使用以下命令运行它

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

现在可以从单独的终端通过telnet命令连接到正在运行的应用程序,aiomonitor将立即响应提示

$ telnet localhost 50101
Asyncio Monitor: 1 tasks running
Type help for commands
monitor >>>

现在您可以输入命令,例如,help

monitor >>> help
Usage: 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还支持在运行的事件循环中内的异步Python控制台,因此您可以探索应用程序的状态

monitor >>> console
Python 3.10.7 (main, Sep  9 2022, 12:31:20) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "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
>>>

要离开控制台,请输入exit()或按Ctrl+D

>>> exit()

✓ The console session is closed.
monitor >>>

扩展

额外的控制台变量

您可以在控制台命令中直接引用更多变量。参考控制台变量示例代码

自定义控制台命令

aiomonitor非常容易通过您自己的控制台命令进行扩展。参考扩展示例代码

要求

CHANGES

0.7.1 (2023-04-16)

  • 通过允许在钩子任务工厂函数中传递可选的namecontext kwargs,正确支持Python 3.11 (#10)

  • 更新开发依赖项

0.7.0 (2022-10-19)

  • 选择性持久终止日志 (#9)

  • 实现取消链追踪器 (#8)

  • 仅在按下Tab键时触发自动完成

  • 支持命令和参数的自动完成 (#7)

  • 添加到Click的缺失显式依赖项

0.6.0 (2022-09-26)

  • 提升console_locals为公共属性

  • 重新实现控制台命令 (#6)

  • 迁移到基于Click的命令行界面 (#5)

  • 采用prompt_toolkit并支持并发客户端 (#4)

  • 在执行ps时显示任务总数 (#3)

  • 使用GitHub Actions自动化CI工作流程,应用black,isort,mypy,flake8

0.5.1 (2022-08-29)

  • 修复“ps”命令输出中任务创建位置

0.5.0 (2022-08-26)

  • 使它与Python 3.10兼容

  • 通过设置自定义任务工厂,将任务创建堆栈链显示添加到“where”命令中 (#1)

  • 更改“ps”命令视图以更简洁且更好地显示许多任务 (#2)

0.4.5 (2019-11-03)

  • 修复EOF上的无限循环(感谢@apatrushev)

0.4.4 (2019-03-23)

  • 简化python控制台启动结束 #175

  • 添加Python 3.7兼容性 #176

0.4.3 (2019-02-02)

  • 重构控制台服务器启动/关闭逻辑 #169

0.4.2 (2019-01-13)

  • 修复从0.4.1版本发布的问题中类型注解的问题 #164

0.4.1 (2019-01-10)

  • 修复Python 3.5支持 #161(感谢@bmerry)

0.4.0 (2019-01-04)

  • 添加对自定义命令的支持 #133(感谢@yggdr)

  • 修复OptLocals被传递为“locals”的默认值 #122(感谢@agronholm)

  • 添加了一个受标准库cmd模块启发的API #135(感谢@yggdr)

  • 正确报告运行aioconsole的端口 #124(感谢@bmerry)

0.3.1 (2018-07-03)

  • 添加了堆栈跟踪命令 #120(感谢@agronholm)

0.3.0 (2017-09-08)

  • 添加了传递环境到python REPL的_local_参数

0.2.1 (2016-01-03)

  • 修复telnet CLI中的导入问题 #12(感谢@hellysmile)

0.2.0 (2016-01-01)

  • 添加基本文档

  • Monitor类的多数方法不是私有API

0.1.0 (2016-12-14)

  • 添加缺少的LICENSE文件

  • 更新API,添加start_monitor()函数

0.0.3 (2016-12-11)

  • 修复README.rst

0.0.2 (2016-12-11)

  • 测试现在更稳定

  • 在README.rst中添加简单教程

0.0.1 (2016-12-10)

  • 初始版本。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

aiomonitor-ng-0.7.2.tar.gz (28.7 kB 查看散列)

上传时间 源代码

构建分发

aiomonitor_ng-0.7.2-py3-none-any.whl (26.0 kB 查看散列)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面