python结束线程_python终止线程
可以通过以下方式来终止线程:通过抛出异常来终止进程
通过一个终止标志来终止进程
使用traces来终止进程
使用多线程模型来终止进程
通过将进程设置为deamon来终止进程
使用隐藏属性_stop()
通过抛出异常来终止进程
# Python program raising
# exceptions in a python
# thread
import threading
import ctypes
import time
class thread_with_exception(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
# target function of the thread class
try:
while True:
print('running ' + self.name)
finally:
print('ended')
def get_id(self):
# returns id of the respective thread
if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id
def raise_exception(self):
thread_id = self.get_id()
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
t1 = thread_with_exception('Thread 1')
t1.start()
time.sleep(2)
t1.raise_exception()
t1.join()
运行以上程序,一旦raise_exception()被调用,run()函数将被终止。这是因为程序抛出异常,将不在try...finally...函数块中运行,run()函数即被终止。
2.通过一个终止标志来终止进程
# Python program showing
# how to kill threads
# using set/reset stop
# flag
import threading
import time
def run():
while True:
print('thread running')
global stop_threads
if stop_threads:
break
stop_threads = False
t1 = threading.Thread(target = run)
t1.start()
time.sleep(1)
stop_threads = True
t1.join()
print('thread killed')
以上代码中,一旦全局变量stop_threads被置位,run()函数将终止,对应t1线程将因为t1.join()被终止。如果不使用全局变量,可以采用下面方式。
# Python program killing
# threads using stop
# flag
import threading
import time
def run(stop):
while True:
print('thread running')
if stop():
break
def main():
stop_threads = False
t1 = threading.Thread(target = run, args =(lambda : stop_threads, ))
t1.start()
time.sleep(1)
stop_threads = True
t1.join()
print('thread killed')
main()
上面代码中传递的函数对象始终返回局部变量stop_threads的值。 在函数run()中检查该值,并且一旦重置stop_threads,run()函数就会结束,并终止线程。
3.使用traces来终止进程
# Python program using
# traces to kill threads
import sys
import trace
import threading
import time
class thread_with_trace(threading.Thread):
def __init__(self, *args, **keywords):
threading.Thread.__init__(self, *args, **keywords)
self.killed = False
def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
def kill(self):
self.killed = True
def func():
while True:
print('thread running')
t1 = thread_with_trace(target = func)
t1.start()
time.sleep(2)
t1.kill()
t1.join()
if not t1.isAlive():
print('thread killed')
在此代码中,对start()进行了一些修改,以使用settrace()设置系统跟踪功能。 定义本地跟踪函数,以便每当设置了相应线程的kill标志(killed)时,在执行下一行代码时都会引发SystemExit异常,从而结束目标函数func的执行。 现在可以使用join()杀死线程。
4.使用多线程模型来终止进程
# Python program creating
# three threads
import threading
import time
# counts from 1 to 9
def func(number):
for i in range(1, 10):
time.sleep(0.01)
print('Thread ' + str(number) + ': prints ' + str(number*i))
# creates 3 threads
for i in range(0, 3):
thread = threading.Thread(target=func, args=(i,))
thread.start()
上面的代码的功能也可以通过使用多处理模块以类似的方式实现,只需要很少的更改即可实现。 请参阅下面给出的代码。
# Python program creating
# thread using multiprocessing
# module
import multiprocessing
import time
def func(number):
for i in range(1, 10):
time.sleep(0.01)
print('Processing ' + str(number) + ': prints ' + str(number*i))
for i in range(0, 3):
process = multiprocessing.Process(target=func, args=(i,))
process.start()
尽管两个模块的接口相似,但是两个模块的实现却大不相同。 所有线程共享全局变量,而进程则彼此完全分开。 因此,与杀死线程相比,杀死进程要安全得多。 为Process类提供了一个终止进程的方法Terminate()。 现在,回到最初的问题。 假设在上面的代码中,我们想在0.03秒过去之后终止所有进程。 使用以下代码中的多处理模块可实现此功能。
# Python program killing
# a thread using multiprocessing
# module
import multiprocessing
import time
def func(number):
for i in range(1, 10):
time.sleep(0.01)
print('Processing ' + str(number) + ': prints ' + str(number*i))
# list of all processes, so that they can be killed afterwards
all_processes = []
for i in range(0, 3):
process = multiprocessing.Process(target=func, args=(i,))
process.start()
all_processes.append(process)
# kill all processes after 0.03s
time.sleep(0.03)
for process in all_processes:
process.terminate()
虽然这两个模块具有不同的实现。 上面代码中的多处理模块提供的此功能类似于杀死线程。 因此,只要需要在Python中实现线程杀死,就可以将多处理模块用作简单的替代方法。
5.通过将进程设置为deamon来终止进程
import threading
import time
import sys
def func():
while True:
time.sleep(0.5)
print('Thread alive, and it won't die on program termination')
t1 = threading.Thread(target=func)
t1.start()
time.sleep(2)
sys.exit()
请注意,线程t1保持活动状态,并防止主程序通过sys.exit()退出。 在Python中,任何活动的非守护线程都会阻止主程序退出。 而守护程序线程本身在主程序退出时就被杀死。 换句话说,一旦主程序退出,所有守护程序线程就会被杀死。 要将线程声明为守护程序,我们将关键字参数daemon设置为True。 对于给定代码中的Example,它演示了守护程序线程的属性。
# Python program killing
# thread using daemon
import threading
import time
import sys
def func():
while True:
time.sleep(0.5)
print('Thread alive, but it will die on program termination')
t1 = threading.Thread(target=func)
t1.daemon = True
t1.start()
time.sleep(2)
sys.exit()
注意,一旦主程序退出,线程t1将被杀死。 在可以使用程序终止来触发线程终止的情况下,该方法被证明非常有用。 请注意,在Python中,一旦所有非守护程序线程都死了,则主程序将终止,而与存活的守护程序线程数无关。 因此,这些守护进程线程所拥有的资源(例如打开的文件,数据库事务等)可能无法正确释放。 python程序中的初始控制线程不是守护程序线程。 除非可以肯定,否则不建议强行杀死线程,因为这样做不会导致任何泄漏或死锁。
6.使用隐藏属性_stop()
为了杀死线程,我们使用了隐藏函数_stop(),该函数没有文档说明,但可能会在下一版本的python中消失。
# Python program killing
# a thread using ._stop()
# function
import time
import threading
class MyThread(threading.Thread):
# Thread class with a _stop() method.
# The thread itself has to check
# regularly for the stopped() condition.
def __init__(self, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
self._stop = threading.Event()
# function using _stop function
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
while True:
if self.stopped():
return
print("Hello, world!")
time.sleep(1)
t1 = MyThread()
t1.start()
time.sleep(5)
t1.stop()
t1.join()
注意:上面的方法在某些情况下可能不起作用,因为python没有提供任何直接的方法来杀死线程。
参考文档:
python结束线程_python终止线程相关推荐
- python结束线程池正在运行的线程_python之线程与线程池
#进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程.#传统的不确切使用线程的程序称为只含有一个线程或单线程程序,而可以使用线程的程序被称为多线程程序,在程序中使用一个线 ...
- python守护线程_Python守护线程用法实例
本文实例讲述了Python守护线程用法.分享给大家供大家参考,具体如下: 如果你设置一个线程为守护线程,就表示你在说这个线程是不重要的,在进程退出的时候,不用等待这个线程退出.如果你的主线程在退出的时 ...
- python开两个守护线程_python 守护线程
守护线程 如果python线程是守护线程,那么以为着这个线程是"不重要"的,"不重要"意味着如果他的父进程结束了但该守护线程没有运行完,守护进程就会被强制结束. ...
- python获取当前线程_Python爬虫(线程,进程)
第一章 线程的使用 并发:指的是任务数多余cpu核数 并行:指的是任务数小于等于cpu核数,即任务真的是一起执行的 1.线程的概念 线程就是在程序运行过程中,执行程序代码的一个分支,每个运行的程序 ...
- python多线程守护线程_Python守护程序线程
python多线程守护线程 In this tutorial we will be learning about Python Daemon Thread. In our previous tutor ...
- python 在主线程开线程_Python开启线程,在函数中开线程的实例
逻辑处理上分成了多个模块,为了提高效率,前一个模块处理完调用后一个模块操作时使用多线程 我这里遇到的情形是前面取数据后面存到mysql,发现单线程效率很低,改为取数据后开线程存到mysql 开启线程之 ...
- python结束循环_python中break、continue 、exit() 、pass终止循环的区别
python中break.continue .exit() .pass区分 1.break:跳出循环,不再执行 Python break语句,就像在C语言中,打破了最小封闭for或while循环. b ...
- python可以开多少线程_Python开启线程,在函数中开线程的实例
逻辑处理上分成了多个模块,为了提高效率,前一个模块处理完调用后一个模块操作时使用多线程 我这里遇到的情形是前面取数据后面存到mysql,发现单线程效率很低,改为取数据后开线程存到mysql 开启线程之 ...
- python内容限制_Python 限制线程的最大数量的方法(Semaphore)
Python 限制线程的最大数量的方法(Semaphore) 更新时间:2019年02月22日 09:33:30 作者:houyanhua1 今天小编就为大家分享一篇Python 限制线程的最大数量的 ...
最新文章
- Centos6搭建SkyWalking 做分布式跟踪
- [react] React的触摸事件有哪几种?
- 命令行开启一个unity实例和执行其中的脚本方法的使用和注意
- stack 的优势 - 每天5分钟玩转 Docker 容器技术(113)
- Backup Exec 2010 R3 灾难恢复 Exchange 2010
- 【数据库】E-R图向关系模型转换的规则
- oracle sql group_con,SQL:Group Functions,GROUP BY,HAVING
- 大数据安全分析的特征有哪些
- 大学离散数学作业用代码怎么写?用Python判断离散数学的自反、对称、传递、符合、自反闭包、函数及其类型直接上代码配超详细注释以及源码下载地址 =_= python大学任务
- Python自学笔记11:实操案例八(咖啡馆、2019中超联赛前五,模拟手机通讯录)
- Facebook广告投放策略与优化Facebook广告成效的技巧方式
- plt.plot()的使用
- 刚从电影院回来,随便写写……
- 基于云端的智能视频监控系统的设计与实现
- 备份阿里云实例-oss-browser
- 【大数据相关】电商大数据之用户画像介绍
- 《痞子衡嵌入式半月刊》 第 47 期
- 风口之上,AI教育的一场“文火慢炖”
- 编码-Windows中UTF8与BOM
- 一劳永逸部署项目:通过tomcat加载环境变量
热门文章
- 外部中断----高低电平触发,(边沿触发)上升沿触发和下降沿触发区别
- flash loader下载使用说明
- 网络协议分析与仿真课程设计报告:网络流量分析与协议模拟
- 天罡现世,Balong出海,华为这波5G动作666!
- ionic4自定义插件
- Django Web 开发极简实战
- C 语言 —— 循环结构
- 前端面试之浏览器原理篇
- 论文笔记—ITS+假名管理—ASPA: Advanced Strong Pseudonym based Authentication in Intelligent Transport System
- Java显示文件层级目录函数_[转载]文件和目录复制函数