python 多线程用于继续循环直到用户输入。帮助理解所要求的示例

标签 python loops user-input python-multithreading

我正在实现一个循环,该循环应该继续下去,直到用户按照 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/

相关文章:

python - 模块未找到错误 : spec not found for the module

python - 当从另一个函数调用 mongodb 时如何模拟 mongodb?

javascript - Cypress - 测试提交无效输入时是否存在特定于浏览器的警报

perl - 为什么我在比较 Perl 中输入的行时遇到问题?

python - 使用 Python Eve Rest 和 Mongo 过滤嵌入式文档

java - 如何在 IF 语句中使用数组?

loops - 在文件夹中搜索包含服务器名称和扩展名的文件,然后选择最后修改的文件,并在文件名中获取日期

r - 如何制作根据条件跨列多次连接的 R 循环

c++ - 有条件地打破一长串输入?

python - 为什么 python 字典中的字符串键的写入/读取速度比元组慢?