码迷,mamicode.com
首页 > 其他好文 > 详细

asyncio 自动跳出长时间堵塞的 task

时间:2019-04-18 20:21:16      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:ons   uri   orm   with   for   current   字符   eve   超过   

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

标签:ons   uri   orm   with   for   current   字符   eve   超过   

原文地址:https://www.cnblogs.com/ywhyme/p/10731771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!