Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
input()函数在python中存在线程时引发错误_Python_Python 3.x - Fatal编程技术网

input()函数在python中存在线程时引发错误

input()函数在python中存在线程时引发错误,python,python-3.x,Python,Python 3.x,以下代码按预期工作。它接受两个相同的输入和输出 import sys import threading def main(): n = int(input("input n:")) parents = list(map(int, input("input parents:").split())) print("n is {0} and parents is {1}".format(n,str(parents))) if __name__ == "__main__":

以下代码按预期工作。它接受两个相同的输入和输出

import sys
import threading
def main():
    n = int(input("input n:"))
    parents = list(map(int, input("input parents:").split()))
    print("n is {0} and parents is {1}".format(n,str(parents)))

if __name__ == "__main__":
    main()
当我添加这段额外的代码以支持更深入的递归和线程时,它抛出了一个值错误。我给出的第一个输入为“3”(不带引号),第二个输入为“-1 0 1”(不带引号)

main()函数从两个位置调用

  • 首先,从线程调用main()函数
  • 其次,将调用\uuuuuuuuuuuuuuuu主\uuuuuuu。这里也调用main()函数
  • 所以你被要求输入“input n”两次。第二次输入字符串值('-1 0 1')时,再次为“input n”赋值。它需要int作为输入。因此,问题正在发生
代码修复: 将线程移到main内,并移除现有的main()


我希望它能帮助您……

您有两个运行在两个线程中的
main()
实例。他们将为用户的输入而斗争;我猜其中一个得到了
3
(仍在等待更多输入),另一个得到了换行符(因此从
input()
)返回一个空字符串)。
import sys
import threading
def main():
    n = int(input("input n:"))
    parents = list(map(int, input("input parents:").split()))
    print("n is {0} and parents is {1}".format(n,str(parents)))

sys.setrecursionlimit(10**7)  # max depth of recursion
threading.stack_size(2**27)   # new thread will get stack of such size
threading.Thread(target=main).start()

if __name__ == "__main__":
    main()
threading.Thread(target=main).start()
if __name__ == "__main__":
        main()
import sys

import threading


def main():
    n = int(input("input n:"))
    parents = list(map(int, input("input parents:").split()))
    print("n is {0} and parents is {1}".format(n, str(parents)))


if __name__ == "__main__":
    sys.setrecursionlimit(10 ** 7)  # max depth of recursion
    threading.stack_size(2 ** 27)  # new thread will get stack of such size
    threading.Thread(target=main).start()