python结束子线程结束
㈠ python如何结束线程
在线程里添加一个结束标识,想要结束线程时吧标识置为True,可以结束线程 Hider和它的父类threading.Thread都没有__stop参数,当然会报错了
㈡ python3 threading模块如何关闭或者退出子线程
Thread 是threading模块中最重要的类之一,可以使用它来创建线程。有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法;
另一种是创建一个threading.Thread对象,在它的初始化函数(__init__)中将可调用对象作为参数传入。下面分别举例说明。
㈢ 弱问python的问题.怎么终止一个子线程
等待串口数据导致线程自己sleep而没有机会执行,主线程的join没法继续,方法就是这样的,换成这个能执行
fromthreadingimport*
importtime
classMyThread(Thread):
defrun(self):
self.ifdo=True;
whileself.ifdo:
print'Iamrunning...'
time.sleep(0.1)
defstop(self):
print'Iwillstopit...'
self.ifdo=False;
tr=MyThread()
tr.setDaemon(True)
tr.start()
time.sleep(1)
tr.stop()
tr.join()
这样就更直观了
fromthreadingimport*
importtime
classMyThread(Thread):
defrun(self):
self.ifdo=True;
whileself.ifdo:
print'Iamrunning...'
time.sleep(2)
defstop(self):
print'Iamstoppingit...'
self.ifdo=False;
tr=MyThread()
tr.setDaemon(True)
tr.start()
print'Iwillstopit...'
time.sleep(5)
tr.stop()
tr.join()
㈣ python 如何结束子线程
等待串口数据导致线程自己sleep而没有机会执行,主线程的join没法继续,方法就是这样的,换成这个能执行
from threading import *import time class MyThread(Thread): def run (self): self.ifdo = True; while self.ifdo: print 'I am running...' time.sleep(0.1) def stop (self): print 'I will stop it...' self.ifdo = False; tr = MyThread()tr.setDaemon(True)tr.start()time.sleep(1)tr.stop()tr.join()
这样就更直观了
from threading import *import time class MyThread(Thread): def run (self): self.ifdo = True; while self.ifdo: print 'I am running...' time.sleep(2) def stop (self): print 'I am stopping it...' self.ifdo = False; tr = MyThread()tr.setDaemon(True)tr.start()print 'I will stop it...'time.sleep(5)tr.stop()tr.join()
㈤ threading python怎么结束
以题主的代码为例,如果题主在fun1里头输出一下这个:
print(dir(t))
应该会出现这个:
<img src="https://pic4.mg.com/_b.png" data-rawwidth="814" data-rawheight="182" class="origin_image zh-lightbox-thumb" width="814" data-original="https://pic4.mg.com/_r.png">看到我标记那个没,调用即可结束线程。比如这样:看到我标记那个没,调用即可结束线程。比如这样:
t._Thread__stop()
这样这个子线程就结束了。
显然这是个双下划线开头的方法,详情如下(有使用注意事项):
def __stop(self):
# DummyThreads delete self.__block, but they have no waiters to
# notify anyway (join() is forbidden on them).
if not hasattr(self, '_Thread__block'):
....
㈥ python 主程序结束的时候线程是否结束问题2个问题
第一个问题:因为主进程已经结束,相关的资源已经释放,而线程还在后台运行,所以会导致线程找不到相关的资源和定义
第二个问题:因为主程序结束的时候,并没有等待子线程结束,也没有强制关闭子线程,因此还在后台运行,有两个办法可以让他们同时结束,一个办法是在在构建进程的时候增加参数 deamon=True, 第二个办法就是在程序最后增加thread1.join(),thread2.join()
㈦ Python 中如何结束子线程
等待串口数据导致线程自己sleep而没有机会执行,主线程的join没法继续,方法就是这样的,换成这个能执行
from threading import *import time class MyThread(Thread): def run (self): self.ifdo = True; while self.ifdo: print 'I am running...' time.sleep(0.1) def stop (self): print 'I will stop it...' self.ifdo = False; tr = MyThread()tr.setDaemon(True)tr.start()time.sleep(1)tr.stop()tr.join()
这样就更直观了
from threading import *import time class MyThread(Thread): def run (self): self.ifdo = True; while self.ifdo: print 'I am running...' time.sleep(2) def stop (self): print 'I am stopping it...' self.ifdo = False; tr = MyThread()tr.setDaemon(True)tr.start()print 'I will stop it...'time.sleep(5)tr.stop()tr.join()
㈧ python编程中线程结束的问题
def _exitCheckfunc():
print "ok"
try:
while 1:
alive=False
if thread_.isAlive():
alive=True
if not alive:
break
time.sleep(1)
#为了使得统计时间能够运行,要捕捉 KeyboardInterrupt :ctrl-c
except KeyboardInterrupt, e:
traceback.print_exc()
print "consume time :",time.time()-start
threading._shutdown=_exitCheckfunc
自己在主线程中写一个死循环来接受ctrl+c的信号。
或者用进程监控 :
http://code.activestate.com/recipes/496735-workaround-for-missed-sigint-in-multithreaded-prog/
㈨ Python中怎么在终止一个线程的同时终止另外一个线程
设置一个全局变量,初值为False
设置键盘监听事件,当监测到特定按键时,将全局变量的值修改为True
在每个子线程中,循环检测全局变量的值,当检测到值为True时退出线程函数。