asyncio 自动跳出长时间堵塞的 task
https://www.cnblogs.com/ywhyme/p/10660411.html 的升级版
可以知道当前是卡在哪一个 task 甚至是多少行
import asyncio
import os
import queue
import signal
import time
import threading
import logging
# logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(filename)s : %(levelname)s %(message)s", )
logging.basicConfig(level=logging.DEBUG)
# 魔改部分
# 魔改原因, 在 loop.debug 为 True 的时候才会给 loop 设置 _current_handle
from asyncio import events
class Handle(events.Handle):
def _run(self):
self._loop._current_handle = self
super()._run()
events.Handle = Handle
# 魔改结束
async def factorial(name, number):
f = 1
for i in range(2, number + 1):
print(f"Task {name}: Compute factorial({i})...")
await asyncio.sleep(1)
f *= i
print(f"Task {name}: factorial({number}) = {f}")
async def test():
for i in range(100):
print("sleep--", i)
time.sleep(1)
def handler(signum, frame):
print('Signal handler called with signal', signum)
raise Exception("Kill the task")
signal.signal(signal.SIGTERM, handler)
async def main():
await asyncio.gather(
test(),
factorial("A", 2),
factorial("B", 3),
factorial("C", 4),
return_exceptions=True
)
def check(co_name, threshold: int = 60) -> bool:
"""连续的记录超过阈值"""
i = 0
for item in q:
if item == co_name:
i += 1
else:
break
if i >= threshold:
return True
else:
return False
def asyncio_monitor(loop, step: int = 1):
while not stop:
if hasattr(loop._current_handle, "_callback"):
callback = loop._current_handle._callback
task = getattr(callback, "__self__") # Task
co_name = task._coro.cr_code.co_name # task._coro.cr_code.co_name # coro name
if check(co_name, 10):
# 长时间堵塞, 抛出异常让 task 结束
if pid != None:
os.system(f"kill -{signal.SIGTERM} {pid}")
if task._state == "PENDING":
q.appendleft(co_name)
# info 为一个的回显字符串
info = str(getattr(callback, "__self__", callback))
print(info)
#
# coro = task._coro.co_name # coro name
# task._state # futures/_base.py:25
#
time.sleep(step)
def run_asyncio(loop):
global stop
# loop.set_debug(True)
loop.run_until_complete(main())
stop = True
if __name__ == '__main__':
pid = os.getpid()
print(pid)
stop = False
q = queue.deque(maxlen=100)
loop = asyncio.get_event_loop()
t1 = threading.Thread(target=asyncio_monitor, args=(loop,))
t1.start()
run_asyncio(loop)
asyncio 自动跳出长时间堵塞的 task的更多相关文章
- 在asyncio 中跳出正在执行的task
需求描述 代码在asyncio的框架中运行, 但是一旦一个task出现了长时间的堵塞,我们要跳过这个task(代码可能是用户输入的,例如用户编写的插件) 代码如下 (其中大部分代码出自官方的 asyn ...
- WPF窗口长时间无人操作鼠标自动隐藏
在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下: 1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面: 2.软件为了 ...
- JavaScript长时间未操作自动退出登录
主要是通过mouseover 来监听有没有进行当前页面操作,通过未操作时间和设定退出的时间做比较,从而退出登录. var oldTime = new Date().getTime(); var new ...
- web页面长时间未操作自动退出登录
var lastTime = new Date().getTime(); var currentTime = new Date().getTime(); * * ; //设置超时时间: 10分 $(f ...
- jdbc 数据库连接 长时间空闲 断开连接 ApplicationContext.xml
数据库连接 长时间空闲 断开连接solution: <property name="validationQuery" value="select 1"/& ...
- wireshark长时间抓包分多个文件
前言 说一说这个问题的由来,一般使用wireshark不需要长时间抓包的,但是有时候遇到网络通信中非常棘手的问题,例如一个小时出现一次或者几个小时出现一次问题的情况,这种情况下就必须长时间抓包了.但是 ...
- ios之申请后台延时执行和做一个假后台的方法(系统进入长时间后台后,再进入前台部分功能不能实现)
转自:http://sis hu ok.com/forum/blogCategory/showByCategory.html?categories_id=138&user_id=10385 ...
- 实现iOS长时间后台的两种方法:Audiosession和VOIP(转)
分类: Iphone2013-01-24 14:03 986人阅读 评论(0) 收藏 举报 我们知道iOS开启后台任务后可以获得最多600秒的执行时间,而一些需要在后台下载或者与服务器保持连接的App ...
- WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z
操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...
随机推荐
- 0923如何利用mysqlbinlog日志闪回
转自 https://github.com/danfengcao/binlog2sql,感谢作者的提供 binlog2sql 从MySQL binlog解析出你要的SQL.根据不同选项,你可以得到原始 ...
- Hardware/Firmware/Software的区别
Hardware: 硬件Firmware: 固化到硬件中的程序Software: 一般指驱动和应用软件,不用固化到硬件中的 ************************************** ...
- libyuv库的使用
libyuv是Google开源的实现各种YUV与RGB之间相互转换.旋转.缩放的库.它是跨平台的,可在Windows.Linux.Mac.Android等操作系统.x86.x64.arm架构上进行编译 ...
- Java异常的处理机制(二)
1.throw的作用 class Usre { private int age; public void setAge (int age) { if(age < 0) { RuntimeExce ...
- linux命令用来查看日志关键字
1.查看日志 前 n行: cat 文件名 | head -n 数量 demo: cat test.log | head -n 200 # 查看test.log前200行 2.查看日志 尾 n行: c ...
- Uva1335 二分+贪心
/* 奇数怎么搞呢 二分到答案怎么judge呢 贪心怎么贪呢 假设贪心方案是 前两个挨着取 后面的能靠前就靠前 这样子似乎保证了ans最min 但是不管贪的对不对 操作起来时间GG 而且 如果真的这样 ...
- android 经典博客
http://blog.csdn.net/harvic880925/article/category/1707319
- 获取id 获取当前点击元素节点的任意 属性
<a id="haveproces" onclick="fnProces(event)" dataid="{{x.id}}" clas ...
- Linux Shell Scripting Cookbook 读书笔记 2
cat,script,find, xargs, tr, tmp文件,字符串截取,批量文件重命名,固定大小文件,自动化交互 1. cat的用法 压缩连续的空白行 cat -s file 也可以用tr,将 ...
- IEnumerable ICollection IList