Python v3.5、Windows 10
我正在使用多个进程并 try catch 用户输入。搜索我看到的所有内容,在多个进程中使用 input()
时会发生奇怪的事情。经过 8 个小时以上的尝试,我实现的任何方法都不起作用,我确信我做错了,但我一生都无法弄清楚。
以下是演示该问题的非常精简的程序。现在,当我在 PyCharm 中运行该程序时,它可以正常工作,但是当我使用 pyinstaller 创建单个可执行文件时,它会失败。该程序不断陷入循环,要求用户输入如下所示的内容: .
我非常确定这与 Windows 如何从我读过的内容中获取标准输入有关。我还尝试将用户输入变量作为 Queue() 项传递给函数,但存在同样的问题。我读到你应该将 input()
放在主 python 进程中,所以我在 if __name__ = '__main__':
from multiprocessing import Process
import time
def func_1(duration_1):
while duration_1 >= 0:
time.sleep(1)
print('Duration_1: %d %s' % (duration_1, 's'))
duration_1 -= 1
def func_2(duration_2):
while duration_2 >= 0:
time.sleep(1)
print('Duration_2: %d %s' % (duration_2, 's'))
duration_2 -= 1
if __name__ == '__main__':
# func_1 user input
while True:
duration_1 = input('Enter a positive integer.')
if duration_1.isdigit():
duration_1 = int(duration_1)
break
else:
print('**Only positive integers accepted**')
continue
# func_2 user input
while True:
duration_2 = input('Enter a positive integer.')
if duration_2.isdigit():
duration_2 = int(duration_2)
break
else:
print('**Only positive integers accepted**')
continue
p1 = Process(target=func_1, args=(duration_1,))
p2 = Process(target=func_2, args=(duration_2,))
p1.start()
p2.start()
p1.join()
p2.join()
最佳答案
当您使用 PyInstaller 生成 Windows 可执行文件时,需要使用 multiprocessing.freeze_support()
。
直接来自 docs :
multiprocessing.freeze_support()
Add support for when a program which uses multiprocessing has been frozen to produce a Windows executable. (Has been tested with py2exe, PyInstaller and cx_Freeze.)
One needs to call this function straight after the if name == 'main' line of the main module. For example:
from multiprocessing import Process, freeze_support
def f():
print('hello world!')
if __name__ == '__main__':
freeze_support()
Process(target=f).start()
If the freeze_support() line is omitted then trying to run the frozen executable will raise RuntimeError.
Calling freeze_support() has no effect when invoked on any operating system other than Windows. In addition, if the module is being run normally by the Python interpreter on Windows (the program has not been frozen), then freeze_support() has no effect.
在您的示例中,您还应该解决不必要的代码重复。
关于python - 进程陷入 PyInstaller 可执行文件循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537293/