我正在实现一个循环,该循环应该继续下去,直到用户按照 this page 上的说明按下 Return 键。我让它工作:
def _input_thread_2( L ):
raw_input()
L.append( None )
#### test _input_thread()
L = []
thread.start_new_thread(_input_thread_2, (L, ))
while True:
time.sleep(1)
print "\nstill going..."
if L:
break
我的问题是为什么下面这个看似简单的适配却行不通?它不会在按下某个键时退出循环,而是继续循环:
def _input_thread_3( keep_going ):
"""
Input: doesn't matter
Description - When user clicks the return key, this changes the input to the False bool.
"""
raw_input()
keep_going = False
#### test _input_thread()
keep_going = True
thread.start_new_thread(_input_thread_3, (keep_going, ) )
while True:
time.sleep(1)
print "\nstill going..."
if not keep_going:
break
你能帮我理解它们之间的区别吗?
最佳答案
In Python, why can a function modify some arguments as perceived by the caller, but not others?
这就是原因。你的keep_alive是不可变的,而列表是可变的。
关于python 多线程用于继续循环直到用户输入。帮助理解所要求的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46324066/