Concurrent.futures > 在命令行中运行良好,而不是在使用 pyinstaller 或 py2exe 编译时

Ste*_*eph 4 python py2exe pyinstaller multiprocessing concurrent.futures

我有一个基于 concurrent.futures 的非常简单的脚本,它在命令行(Python 2.7)中运行良好,但是在使用 py2exe 或 Pyinstaller 编译时崩溃(编译后的程序会打开越来越多的进程,如果我不这样做,最终会完全阻塞窗口)先杀了他们)。

代码非常标准/简单,所以我很难理解这个问题的起源......有没有人更早经历过这个?(我发现与多处理的类似问题相关的讨论......但没有什么可以用来解决我的问题)

# -*- coding: utf8 -*-
import os
import socket
import concurrent.futures

def simple_checkDomain(aDomain):
    print aDomain 
    # Do other stuff

def main():

    with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
        for domain in ["google.com","yahoo.com"]:
            job = executor.submit(simple_checkDomain, domain)

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

最好的问候,S

Ron*_*xão 8

补充 rdp 的答案,它从有关冻结支持的多处理文档中获取线索:

您需要做的是将这些行添加到应用程序执行的最开始处:

from multiprocessing import freeze_support

if __name__ == '__main__':
    freeze_support()
Run Code Online (Sandbox Code Playgroud)

还有来自文档:

需要在if __name__ == '__main__'主模块行之后直接调用此函数。

在我的测试中,我发现我需要将它们添加到主应用程序模块中,而不是使用的模块中concurrent.futures,这意味着当进程分叉时,可执行文件将再次启动,并且该模块将在执行恢复到之前运行水池。这对于我的情况尤其重要,因为 PyQt 应用程序会尝试启动一个新的 GUI。

作为旁注,其他答案链接中的错误报告表明可能RuntimeError没有提出,这就是我的情况。


rdp*_*rdp 5

我在 Python 3.4 中遇到了cx_freeze 的这个问题。经过更多的谷歌搜索,我找到了这个错误报告:http : //bugs.python.org/issue21505

这将我发送到以下文档,在我的应用程序中,它似乎解决了这个问题。

Python 2.7 的建议解决方案,直接来自文档:multiprocessing.freeze_support

不确定这是否会为您正在使用的 py2exe 或 Pyinstaller 修复它,但我想我会发布以防万一。

  • 它将解决这个问题。当你添加答案时我正在测试它:) (2认同)