python - 进程陷入 PyInstaller 可执行文件循环

标签 python windows parallel-processing pyinstaller python-multiprocessing

Python v3.5、Windows 10

我正在使用多个进程并 try catch 用户输入。搜索我看到的所有内容,在多个进程中使用 input() 时会发生奇怪的事情。经过 8 个小时以上的尝试,我实现的任何方法都不起作用,我确信我做错了,但我一生都无法弄清楚。

以下是演示该问题的非常精简的程序。现在,当我在 PyCharm 中运行该程序时,它可以正常工作,但是当我使用 pyinstaller 创建单个可执行文件时,它会失败。该程序不断陷入循环,要求用户输入如下所示的内容:enter image description here .

我非常确定这与 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/

相关文章:

c# - 并行化 IO 绑定(bind)(网络)ForEach 循环

haskell - 平行 cabal

python - 如何在读取时不写入文件,反之亦然

python - 如何在 python 上导入 pcap 或 pyshark

python - PyQT5为什么get请求响应总是为null

python - scrapy __init__ arg 中的值错误

windows - 导入 IIS 配置导致 IIS 挂起

c# - 无法打开文件流进行读取,但我仍然可以复制文件?

windows - powershell脚本中引用默认路径

python - 编码风格 - 将括号保持在同一行或新行上?