跳转到主要内容

为asyncio应用程序添加监控和Python REPL功能

项目描述

aiomonitor

GitHub Actions status for the main branch codecov.io status for the main branch Latest PyPI package version Downloads count Documentation Status

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

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

An example to run the aiomonitor shell

安装

安装过程很简单,只需

$ pip install aiomonitor

示例

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

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 20101

或包含的 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):
    print('Start sleeping')
    await asyncio.sleep(100)
    return web.Response(text="Simple answer")

loop = asyncio.get_event_loop()
# create application and register route
app = web.Application()
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, loop=loop)

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

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

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

$ telnet localhost 20101
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 >>>

扩展

额外的控制台变量

您可能可以添加更多变量,这些变量可以直接在 console 命令中引用。参考 控制台变量示例代码

自定义控制台命令

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

需求

变更

0.7.0 (2023-12-21)

  • 重新编写了文档(#393

  • 采用 ruff 以替换 black、flake8 和 isort(#391

  • 添加了一个新的演示示例,以展示 aiomonitor 的各种功能,特别是使用 GUI(也用于 PyCon APAC 2023 演讲)(#385

  • 放宽了对 aiohttp 的直接依赖版本范围(“仅 3.8.5”到“3.8.5 和更高版本”),以便在 Python 3.12 上安装(#389

  • 更新了 README 示例,以符合最新的 API 和约定(#383

0.6.0 (2023-08-27)

  • 将基于 Web 的监控用户界面添加到列表、检查和取消正在运行/已终止的任务中,重构了监控的业务逻辑和表示层(termuiwebui)(#84

  • 替换终端UI、Web UI和控制台访问的默认端口号(50101、50201、50102 -> 20101、20102、20103)(#374

  • 采用towncrier自动生成变更日志(#375

0.5.0 (2023-07-21)

  • 修复由于#10导致的Python 3.10回归问题(#11

  • 通过允许在钩子任务工厂函数中传递给asyncio.create_task()的可选参数(name和context kwargs)来正确支持Python 3.11(#10

  • 更新开发依赖项

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

  • 实现取消链跟踪器(#8

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

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

  • 向Click添加缺少的显式依赖项

  • 将console_locals提升为公共属性

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

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

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

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

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

  • 修复'ps'命令输出中任务创建位置的问题

  • 从所有异步调用中移除loop=loop以支持更新的Python版本(#329

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

这些是从[aiomonitor-ng](https://github.com/achimnol/aiomonitor-ng)回滚的变化。由于分支的版本提升已经远离,所有那些额外发布都被压缩到v0.5.0版本中。

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)

  • 添加 locals_ 参数以将环境传递到python REPL

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-0.7.0.tar.gz (2.9 MB 查看散列值)

上传于

构建分布

aiomonitor-0.7.0-py3-none-any.whl (189.6 kB 查看散列值)

上传于 Python 3

支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面