Pytrhon结束死循环的子线程

Python在子线程无线循环的过程中,如果直接ctrl+c结束程序的话,虽然程序可以结束,但是会导致子线程资源无法回收,一般情况不会有太大影响,但是使用TCP通信的时候,子线程是占用特定的端口的,在资源没有回收的情况下,再次启动程序就会报端口占用错误。

所以参考https://blog.csdn.net/hp_cpp/article/details/83040162博主的方法如下强制结束子线程:

import inspect
import ctypes
from threading import Thread

def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")

def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)


def Receive():
    while 1:
        print(1)
        time.sleep(0.1)


def Send():
    while 1:
        print(2)
        time.sleep(0.1)


if __name__ == "__main__":
    t1 = Thread(target=Receive)
    t2 = Thread(target=Send)
    t1.start()
    t2.start()
    stop_thread(t1)
    stop_thread(t2)

以下是我的测试程序,结合signal方法,主线程一直循环,一旦检测到按下Ctrl+c,就会打印出'You pressed Ctrl+C!'这句话,并且调用上述方法结束两个无限循环的子线程,最后调用exit()方法优雅的结束整个程序

import inspect
import ctypes
import signal
import sys
import time
from threading import Thread


def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")


def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)


def signal_handler(signal, frame):
    print('You pressed Ctrl+C!')
    stop_thread(t1)
    stop_thread(t2)
    sys.exit()


def Receive():
    while 1:
        print(1)
        time.sleep(0.1)


def Send():
    while 1:
        print(2)
        time.sleep(0.1)


if __name__ == "__main__":
    t1 = Thread(target=Receive)
    t2 = Thread(target=Send)
    t1.start()
    t2.start()
    print(3)
    while True:
        signal.signal(signal.SIGINT, signal_handler)
        print(4)
        time.sleep(0.1)

猜你喜欢

转载自blog.csdn.net/qq_34935373/article/details/89184484
今日推荐