简体   繁体   English

在 Windows 上降低 multiprocessing.Pool 的进程优先级

[英]Lowering process priority of multiprocessing.Pool on Windows

I use multiprocessing.Pool() to parallelize some heavy Pandas processing but find that it is a bit too successful.我使用multiprocessing.Pool()来并行化一些繁重的 Pandas 处理,但发现它有点太成功了。 My CPU usage goes to 100% and my entire computer becomes very unresponsive.我的 CPU 使用率达到 100%,我的整个计算机变得非常无响应。 Even the mouse becomes difficult to use.甚至鼠标也变得难以使用。

I can change the process priority of my process with this code.我可以使用此代码更改进程的进程优先级。

import psutil
p = psutil.Process(os.getpid())
p.nice = psutil.BELOW_NORMAL_PRIORITY_CLASS

However, when I look in Windows Task Manager I find that only the main python.exe process has been changed to below normal priority.但是,当我查看 Windows 任务管理器时,我发现只有主要的 python.exe 进程已更改为低于正常优先级。

Is there a good way to reduce the priority of the pool processes?有没有降低池进程优先级的好方法?

You can try setting priority of your process' children after you spawned them.您可以尝试在生成它们后设置进程的子进程的优先级。 Something like:就像是:

import psutil

# spawn children and/or launch process pool here

parent = psutil.Process()
parent.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
for child in parent.children():
    child.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)

The same result as by using the answer by @Giampaolo Rodolà is achieved simply by setting the parent process priority before spawning the children:与使用@Giampaolo Rodolà 的答案相同的结果是通过在生成子进程之前设置父进程优先级来实现的:

import psutil

parent = psutil.Process()
parent.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
# the rest of your code

The children processes will inherit the parent's priority.子进程将继承父进程的优先级。 If, however, the parent is to be set to different priority than the children, then the code provided by @Giampaolo Rodolà is needed.但是,如果要将父级设置为与子级不同的优先级,则需要@Giampaolo Rodolà 提供的代码。

The Python documentation states that when a pool is created you can specify the number of processes. Python 文档指出,在创建池时,您可以指定进程数。 If you don't, it will default to os.cpu_count. 如果不这样做,它将默认为 os.cpu_count。 Consequently, you get the expected behavior that all the available logical cores are used.因此,您将获得使用所有可用逻辑核心的预期行为。 In turn, the computer becomes unresponsive.反过来,计算机变得无响应。

It would probably be better to do something simpler by just controlling the number of processes created.通过控制创建的进程数量来做一些更简单的事情可能会更好。 A rule of thumb is to reserve 2 to 4 logical cores for interactive processing.经验法则是为交互式处理保留 2 到 4 个逻辑核心。

Also, the Python documentation states " This number [os.cpu_count()] is not equivalent to the number of CPUs the current process can use. The number of usable CPUs can be obtained with len(os.sched_getaffinity(0)) "此外,Python 文档指出“这个数字 [os.cpu_count()] 不等于当前进程可以使用的 CPU 数量。可用 CPU 的数量可以通过 len(os.sched_getaffinity(0)) 获得

There are several other details that need to be addressed.还有其他几个细节需要解决。 I have tried to capture them at this gist .我试图在这个要点上捕捉它们。 All that you have to do is change LOGICAL_CORES_RESERVED_FOR_INTERACTIVE_PROCESSING for your particular use case.您所要做的就是针对您的特定用例更改 LOGICAL_CORES_RESERVED_FOR_INTERACTIVE_PROCESSING。

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

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