联系方式
学习资讯
python终止线程有哪些方法
python终止线程有哪些方法
方法一:
class BlockingTestThread(Thread):
def __init__(self):
self._running_flag = False
self.stop = threading.Event()
Thread.__init__(self, target=self.test_method)
def test_method(self):
try:
while(not self.swait(1)):
self._running_flag = True
print 'Start wait'
self.swait(100)
print 'Done waiting'
finally:
self._running_flag = False
def terminate(self):
self.sset()
if __name__ == "__main__":
thread = BlockingTestThread()
thread.start()
time.sleep(2)
print 'Time sleep 2'
thread.terminate()
print "Joining thread"
thread.join()
print "Done Joining thread"
方法二:
class Example(object):
def __init__(self):
self.stop = threading.Event()
self.connection = Connection()
self.mythread = Thread(target=self.dowork)
self.mythread.start()
def dowork(self):
while(not self.sis_set()):
try:
blockingcall()
except CommunicationException:
pass
def terminate():
self.sset()
self.connection.close()
self.mythread.join()
更多培训课程,学习资讯,课程优惠等学校信息,请进入 南昌博为峰教育 网站详细了解,免费咨询电话:400-998-6158