资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python终止线程的方法

小编给大家分享一下python终止线程的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

超过十载行业经验,技术领先,服务至上的经营模式,全靠网络和口碑获得客户,为自己降低成本,也就是为客户降低成本。到目前业务范围包括了:成都网站制作、成都网站建设、外贸营销网站建设,成都网站推广,成都网站优化,整体网络托管,小程序制作,微信开发,APP应用开发,同时也可以让客户的网站和网络营销和我们一样获得订单和生意!

在python中启动和关闭线程:

一、启动线程

首先导入threading

import threading

然后定义一个方法

    def serial_read():
   		...
   		...

然后定义线程,target指向要执行的方法

myThread = threading.Thread(target=serial_read)

启动它

myThread.start()

二、停止线程

import inspect
import ctypes
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)

停止线程

stop_thread(myThread)

stop方法可以强行终止正在运行或挂起的线程。

看完了这篇文章,相信你对python终止线程的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网页标题:python终止线程的方法
文章URL:http://cdkjz.cn/article/jodood.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220