4

我有一个非常简单的基于 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()

最好的问候, S

4

3 回答 3

5

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

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

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

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

于 2015-03-17T15:11:31.890 回答
5

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

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

from multiprocessing import freeze_support

if __name__ == '__main__':
    freeze_support()

还有来自文档:

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

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

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

于 2020-01-15T14:15:24.913 回答
1

这个改变对我有用,我使用的是 python 3.6+。

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

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__":
    freeze_support()
    main()
于 2020-08-07T18:53:54.873 回答