Python线程终止方法

本文详细介绍了在Python中使用事件、线程状态和通过异常来终止线程的三种方法,分别展示了如何通过设置事件、改变线程状态以及在子线程中抛出异常来实现线程的终止。
摘要由CSDN通过智能技术生成

Python线程终止方法

Python线程终止有很多思路,本文介绍使用事件终止、状态终止和异常终止3种思路。事件终止和状态终止必须等待耗时任务结束后才能结束,异常终止可以立刻终止。

1 事件终止

借助线程种的事件,终止线程。

import ctypes
import inspect
import threading
import time


class StopThread(threading.Thread):
    def __init__(self):
        super().__init__()

        # 设置停止事件
        self.stop_event = threading.Event()

    def run(self):
        # 清空事件
        self.stop_event.clear()

        i = 0
        # 判断是否终止
        while not self.stop_event.is_set():
            i = i + 1
            print("Start=" + str(i))
            time.sleep(1)
            print("Task=" + str(i))
            time.sleep(1)
            print("end=" + str(i))
            print("====")

    # 终止线程
    def stop(self):
        self.stop_event.set()


def stop_thread_now(thread_id, except_type):
    """
    通过C语言的库抛出异常
    :param thread_id: 线程id
    :param except_type: 异常抛出类型
    :return:
    """
    # 在子线程内部抛出一个异常结束线程
    thread_id = ctypes.c_long(thread_id)
    if not inspect.isclass(except_type):
        except_type = type(except_type)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.py_object(except_type))
    if res == 0:
        raise ValueError("线程id违法")
    elif res != 1:
        ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, None)
        raise SystemError("异常抛出失败")


if __name__ == '__main__':
    # 创建线程
    stop_thread = StopThread()
    stop_thread.start()
    time.sleep(3)

    # 停止线程,需要等待循环完成,不能立刻中断
    stop_thread.stop()

    # 停止线程,立刻终止
    # stop_thread_now(stop_thread.ident, SystemExit)

2 状态终止

状态终止的思路和事件终止思路相同。

import threading
import time


class StopThread(threading.Thread):
    def __init__(self):
        super().__init__()
        self.status = True

    def run(self):
        i = 0
        while self.status:
            i = i + 1
            print("Start=" + str(i))
            time.sleep(1)
            print("Task=" + str(i))
            time.sleep(1)
            print("end=" + str(i))
            print("====")

    # 终止线程
    def stop(self):
        self.status = False


if __name__ == '__main__':
    # 创建线程
    stop_thread = StopThread()
    stop_thread.start()

    # 停止线程,需要等待循环完成,不能立刻中断
    time.sleep(3)
    stop_thread.stop()

3 异常终止

import ctypes
import inspect
import threading
import time


def run_task(param_inner, update_task_inner):
    """
    运行任务
    :param param_inner: 输入的参数
    :param update_task_inner: 输入的方法,停止任务的回调函数
    :return:
    """
    print(param_inner)
    i = 0
    while True:
        i = i + 1
        print("Start=" + str(i))
        time.sleep(1)
        print("Task=" + str(i))
        time.sleep(1)
        print("end=" + str(i))
        # 回调方法,
        update_task_inner("finish=" + str(i))
        print("====")


def stop_thread_now(thread_id, except_type):
    """
    通过C语言的库抛出异常
    :param thread_id: 线程id
    :param except_type: 异常抛出类型
    :return:
    """
    # 在子线程内部抛出一个异常结束线程
    thread_id = ctypes.c_long(thread_id)
    if not inspect.isclass(except_type):
        except_type = type(except_type)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.py_object(except_type))
    if res == 0:
        raise ValueError("线程id违法")
    elif res != 1:
        ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, None)
        raise SystemError("异常抛出失败")


def update_task(msg):
    print(msg)
    pass


if __name__ == '__main__':
    param = "test"
    task_thread = threading.Thread(
        # 运行的线程
        target=run_task,
        # 输入参数
        args=(param, update_task)
    )
    task_thread.start()

    time.sleep(4)

    # 立刻中断线程
    stop_thread_now(task_thread.ident, SystemExit)
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值