Windows 10 上的 Python 多处理 [英] Python multiprocessing on Windows 10

查看:28
本文介绍了Windows 10 上的 Python 多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 7 电脑和 Windows 10 笔记本电脑上运行一些代码:

I'm running some code on both a Windows 7 pc and a Windows 10 laptop:

def project(filename):
    **Do Something Here**


if __name__ == '__main__':
    pool = Pool(processes=4)
    results = [pool.apply_async(project, args=(filename,)) for filename in filenamelist]
    output = [p.get() for p in results]
    print output

两台计算机都是双核/4 线程,因此它们都应该可以很好地运行 4 个进程.我遇到的问题是,当我在 Windows 10 上运行代码时,我运行了 4 个 python 进程,但它们使用 0% 的 cpu,并且它们不会输出任何东西,这与 Windows 7 pc 不同,它将在所有 4线程和完美的工作.

Both computers are dual core/4 threads, so they should both be fine running 4 processes. The problem I have is that when I run the code on Windows 10 I get 4 python processes running, but they use 0% of the cpu and they will not output anything, unlike the Windows 7 pc which will run at full usage on all 4 threads and work perfectly.

如果我不使用多处理,代码在 Windows 10 笔记本电脑上运行良好,所以问题一定与此有关.使用 Python 进行多处理还不能在 Windows 10 中运行吗?顺便说一句,我在两台机器上都运行 Python 2.7.

The code works fine on the Windows 10 laptop if I don't use multiprocessing, so the problem must be related to that. Does multiprocessing with Python not work in Windows 10 yet? I am running Python 2.7 on both machines by the way.

:Windows 7 电脑处理器是 i5-650,Windows 10 笔记本电脑处理器是 i3-2370M

: Windows 7 pc processor is an i5-650, Windows 10 laptop processor is an i3-2370M

[更新]:我将笔记本电脑恢复到 Windows 8.1,并且运行完全相同的代码,这绝对是 Windows 10 的问题.

[Update]: I reverted the laptop back to Windows 8.1 and the exact same code runs as intended, this is definitely a Windows 10 issue.

:我用来生成 filenamelist 的方法如下,但是这在 Windows 7 上运行良好.

: The method I'm using to generate the filenamelist is as follows, however this works fine on Windows 7.

def get_unfinished_files(indir, outdir):
    filenamelist = []
    for filename in os.listdir(indir):
        if filename not in os.listdir(outdir) and filename.endswith('.png'):
            filenamelist.append(filename)
    return filenamelist

推荐答案

遇到了类似的问题.正如我发现的那样,在我的情况下,这只是 python 中的一个错误:https://bugs.python.org/issue35797

Had similar issue. As I found, it was just a bug in python in my case: https://bugs.python.org/issue35797

通过venv使用multiprocessing时发生.

Bugfix 在 Python 3.7.3 中发布.

Bugfix is released in Python 3.7.3.

这篇关于Windows 10 上的 Python 多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆