aiomonitor为异步应用程序添加了监视器和python repl功能
aiomonitor的Python项目详细描述
空气监测器
aiomonitor是python 3.5+模块,添加了监视器和cli功能 用于asyncio应用程序。从curio项目中借用的思想和代码。 同时运行到asyncio循环(或快速插入)的任务监视器 在单独的线程中替换uvloop)作为结果监视器将工作,即使 事件循环由于某种原因被阻塞。
库使用aioconsole模块提供了一个python控制台,这是可能的 在运行的应用程序中执行异步命令。可扩展的 使用自己的命令,使用标准库的cmd模块的样式
安装
安装过程很简单,只要:
$ pip install aiomonitor
示例
监视器具有上下文管理器接口:
importaiomonitorloop=asyncio.get_event_loop()withaiomonitor.start_monitor(loop=loop):loop.run_forever()
现在可以从单独的终端连接到应用程序:
$ nc localhost 50101
或者使用附带的python客户端:
$ python -m aiomonitor.cli
教程
让我们创建简单的aiohttp应用程序,并查看aiomonitor如何 与之整合。
importasyncioimportaiomonitorfromaiohttpimportweb# Simple handler that returns response after 100sasyncdefsimple(request):loop=request.app.loopprint('Start sleeping')awaitasyncio.sleep(100,loop=loop)returnweb.Response(text="Simple answer")loop=asyncio.get_event_loop()# create application and register routeapp=web.Application(loop=loop)app.router.add_get('/simple',simple)# it is possible to pass 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)
让我们将此代码保存在文件simple_srv.py中,以便我们可以使用命令运行它:
$ python simple_srv.py ======== Running on http://localhost:8090 ======== (Press CTRL+C to quit)
现在可以从单独的终端连接正在运行的应用程序 nc命令,立即aiomonitor将响应提示:
$ nc localhost 50101 Asyncio Monitor: 1 tasks running Type help for commands monitor >>>
现在您可以键入命令,例如help:
monitor >>> help Commands: ps : Show task table where taskid : Show stack frames for a task cancel taskid : Cancel an indicated task signal signame : Send a Unix signal stacktrace : Print a stack trace from the event loop thread console : Switch to async Python REPL quit : Leave the monitor
aiomonitor还支持运行事件循环中的异步python控制台 因此您可以探索应用程序的状态:
monitor >>> console Python 3.5.2 (default, Oct 11 2016, 05:05:28) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] 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, loop=loop) --- >>> await asyncio.sleep(1, result=3, loop=loop)
要离开控制台,请键入exit():
>>> exit() monitor >>>
aiomonitor很容易用自己的控制台命令进行扩展。
classWebMonitor(aiomonitor.Monitor):defdo_hello(self,sin,sout,name=None):"""Using the /hello GET interface There is one optional argument, "name". This name argument must be provided with proper URL excape codes, like %20 for spaces. """name=''ifnameisNoneelse'/'+namer=requests.get('http://localhost:8090/hello'+name)sout.write(r.text+'\n')
要求
- Python3.5+
- aioconsole
- uvloop(可选)
更改
0.4.4(2019-03-23)
- 简化的python控制台起始端175
- 增加了Python3.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)
- 修复了Python3.5支持161(感谢@bmerry)
0.4.0(2019-01-04)
- 添加了对自定义命令133的支持(感谢@yggdr)
- 修复了作为“locals”122的默认值传递的optlocals(感谢@agronholm)
- 添加了一个API,灵感来自标准库的CMD模块135(感谢@yggdr)
- 正确报告运行aioconsole 124的端口(感谢@bmerry)
0.3.1(2018-07-03)
- 添加了stacktrace命令120(感谢@agronholm)
0.3.0(2017-09-08)
- 添加了用于将环境传递给python repl的参数
0.2.1(2016-01-03)
- 修复了12中telnet cli中的导入(感谢@hellysmile)
0.2.0(2016-01-01)
- 添加了基本文档
- monitor类的大多数方法都不是私有api
0.1.0(2016-12-14)
- 添加了丢失的许可证文件
- 更新了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)
- 初次发布。