RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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终止线程的方法
文章起源:http://sczitong.cn/article/jodood.html