Pytrhon结束死循环的子线程

本文介绍了一种在Python中优雅地结束子线程的方法,避免了直接Ctrl+C导致的资源未回收问题,特别是在TCP通信场景下。通过使用ctypes模块的PyThreadState_SetAsyncExc函数,可以强制子线程抛出异常并终止,结合signal模块,实现主线程捕获Ctrl+C信号后优雅退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

# 我的套接字的端口是8899
$ netstat -nap | grep 8899
# 会显示进程号port
$ kill -9  port

 

所以参考https://blog.youkuaiyun.com/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)

 

转载于:https://www.cnblogs.com/dingyc/p/10683681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值