Windows 10上的Python多处理 [英] Python multiprocessing on Windows 10
问题描述
我同时在Windows 7 PC和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 pc处理器是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 使用多重处理时发生.
Bugfix在Python 3.7.3中发布.
Bugfix is released in Python 3.7.3.
这篇关于Windows 10上的Python多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!