I want to end a loop running in a separate thread using a global variable. but this code does not seem to stop the thread in loop. I expect the program not to print any more '.' after 2 seconds, but it still runs indefinitely.
我希望结束使用全局变量在单独的线程中运行的循环。但是这段代码似乎并没有停止循环中的线程。我希望这个程序不要再打印了。两秒钟后,它仍然可以无限运行。
Am I doing something fundamentally wrong here?
我做错了什么吗?
import time
import threading
run = True
def foo():
while run:
print '.',
t1 = threading.Thread(target=foo)
t1.run()
time.sleep(2)
run = False
print 'run=False'
while True:
pass
3 个解决方案
#1
5
-
You are executing
foo()
on the main thread by callingt1.run()
. You should callt1.start()
instead.通过调用t1.run()在主线程上执行foo()。应该调用t1.start()。
-
You have two definitions of
foo()
- doesn't matter, but shouldn't be there.foo()有两个定义-没关系,但是不应该在那里。
-
You didn't put a
sleep()
inside the thread loop (in foo()). This is very bad, since it hogs the processor. You should at least puttime.sleep(0)
(release time slice to other threads) if not make it sleep a little longer.您没有在线程循环(foo()中放入sleep()。这很糟糕,因为它占用处理器。您至少应该将time.sleep(0)(向其他线程释放time slice)设置为如果不使其睡眠时间更长一点的话。
Here's a working example:
这里有一个工作示例:
import time
import threading
run = True
def foo():
while run:
print '.',
time.sleep(0)
t1 = threading.Thread(target=foo)
t1.start()
time.sleep(2)
run = False
print 'run=False'
while True:
pass
#2
4
You don't start a thread by calling run()
, you start it by calling start()
. Fixing that made it work for me.
您不会通过调用run()启动一个线程,您可以通过调用start()来启动它。修好它让我工作。
#3
0
In addition to the answers given ...
除了给出的答案……
The variable 'run' is a global variable.
变量“run”是一个全局变量。
When you modify it within another function, e.g. within the main() function, you must make reference to the variable being global otherwise it will not be globally modified.
当您在另一个函数中修改它时,例如在main()函数中,您必须引用全局变量,否则它将不会被全局修改。
def main():
global run
...
run = False
...
if __name__ == "__main__":
main()
#1
5
-
You are executing
foo()
on the main thread by callingt1.run()
. You should callt1.start()
instead.通过调用t1.run()在主线程上执行foo()。应该调用t1.start()。
-
You have two definitions of
foo()
- doesn't matter, but shouldn't be there.foo()有两个定义-没关系,但是不应该在那里。
-
You didn't put a
sleep()
inside the thread loop (in foo()). This is very bad, since it hogs the processor. You should at least puttime.sleep(0)
(release time slice to other threads) if not make it sleep a little longer.您没有在线程循环(foo()中放入sleep()。这很糟糕,因为它占用处理器。您至少应该将time.sleep(0)(向其他线程释放time slice)设置为如果不使其睡眠时间更长一点的话。
Here's a working example:
这里有一个工作示例:
import time
import threading
run = True
def foo():
while run:
print '.',
time.sleep(0)
t1 = threading.Thread(target=foo)
t1.start()
time.sleep(2)
run = False
print 'run=False'
while True:
pass
#2
4
You don't start a thread by calling run()
, you start it by calling start()
. Fixing that made it work for me.
您不会通过调用run()启动一个线程,您可以通过调用start()来启动它。修好它让我工作。
#3
0
In addition to the answers given ...
除了给出的答案……
The variable 'run' is a global variable.
变量“run”是一个全局变量。
When you modify it within another function, e.g. within the main() function, you must make reference to the variable being global otherwise it will not be globally modified.
当您在另一个函数中修改它时,例如在main()函数中,您必须引用全局变量,否则它将不会被全局修改。
def main():
global run
...
run = False
...
if __name__ == "__main__":
main()