2011-08-13 51 views
0

当某些变量值设置为true时如何终止线程?当某些变量设置为true时立即终止线程

import threading 
class test(threading.Thread): 
    def __init__(self): 
     self.stopThread = False 
     self.start(): 
    def start(self): 
     while not self.stopThread: 
       callOtherFunction //which takes alomst 30 sec to execute 
    def stop(self): 
     self.stopThread = True 

现在的问题是,如果启动函数被调用和while循环开始那么它会检查下一个迭代的停止条件,当它完成了它的内部工作,所以如果呼叫到callOtherFunction做出那么仍然等待30秒退出..我想立即终止线程时,变量设置。可能吗?

+0

可能重复[有什么办法杀死Python中的线程?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in- python) –

+0

或者你应该使用多处理,如果你想立即杀死 –

回答

0

这个问题在这里回答:

Is there any way to kill a Thread in Python?

的底线是,如果你能帮助它,你应该避免杀死线程这种方式。但是,如果你必须的话,你可以尝试一些技巧。

而且,是线程安全的,你应该做一个self.stopThreadthreading.Event()使用set()clear()当线程应停止信号。