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

问题描述 投票:2回答:2

我有一个基于current.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

python multiprocessing py2exe pyinstaller concurrent.futures
2个回答
4
投票

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

这使我进入了以下文档,并且在我的应用中,似乎可以解决此问题。

直接从文档中为Python 2.7建议的解决方案:multiprocessing.freeze_support

不知道这是否能解决您正在使用的py2exe或Pyinstaller,但认为我会以防万一。


0
投票

补充rdp的答案,该信息来自多处理文档中有关freeze support的线索:

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

from multiprocessing import freeze_support

if __name__ == '__main__':
    freeze_support()

也来自文档:

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

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

作为旁注,link from the other answer中的错误报告表明RuntimeError可能没有被提出,这就是我的情况。

© www.soinside.com 2019 - 2024. All rights reserved.