简体   繁体   English

Python 多处理。池工作人员保持空闲

[英]Python multiprocessing.Pool workers remain idle

I am a beginner with Python.我是 Python 的初学者。 I am trying to run a simple function using multiprocessing.Pool, but when I run the program, it does not get processed.我正在尝试使用 multiprocessing.Pool 运行一个简单的 function,但是当我运行该程序时,它没有得到处理。 I can see that distinct processes are spawned, but that they remain idle .我可以看到产生了不同的进程,但它们仍然处于空闲状态 For example, the code below does does generate two worker processes but no output and nothing else:例如,下面的代码确实生成了两个工作进程,但没有 output 并且没有别的:

import multiprocessing

def f(x):
    print("Process "+str(x))
    return True

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=2)            
    result = pool.map(f, range(1000)) 

I am running Python 3.9 using Microsoft Visual Studio 2019. OS is Windows 10. I encountered the same problem using Python 3.7 on this computer.我正在使用 Microsoft Visual Studio 2019 运行 Python 3.9。操作系统是 Windows 10。我在这台计算机上使用 Python 3 时遇到了同样的问题。 I have tried the same code on another computer (Also Windows 10, VS2019) and it works fine.我在另一台计算机上尝试了相同的代码(还有 Windows 10,VS2019),它工作正常。 The difference is that this one has an i9 Processor , while the other has an i7.不同的是,这个有一个i9 处理器,而另一个有一个 i7。

I have searched online, but I have not encountered any similar issue or any fix for my problem.我在网上搜索过,但我没有遇到任何类似的问题或任何解决我的问题的方法。 Does anyone have any idea why does this happen and/or possible fixes?有谁知道为什么会发生这种情况和/或可能的修复? May the type of processor have something to do with it?处理器的类型可能与它有关吗? Thanks!谢谢!

This is a just a guess:这只是一个猜测:

I know that this will not run under Jupyter Notebook or the interactive interpreter.我知道这不会在 Jupyter Notebook 或交互式解释器下运行。 See the multiprocessing docs , in particular the section:请参阅多处理文档,特别是以下部分:

在此处输入图像描述

The solution for those environments is to take your worker function, f , and place it in a separate module, for example worker.py , and to import the worker function:这些环境的解决方案是将您的工人 function, f放在一个单独的模块中,例如worker.py ,然后导入工人 function:

import multiprocessing
from worker import f

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=2)
    result = pool.map(f, range(1000))          

Give this a shot and let us know for others who also have Visual Studio whether this worked or not.试一试,让我们知道其他也有 Visual Studio 的人是否有效。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM