問題描述
我正在 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 多處理的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!