英文:
Set niceness of each process in a multiprocessing.Pool
问题
如何为multiprocessing.Pool
中的每个进程设置niceness?我了解可以使用os.nice()
增加niceness,但在创建池后如何在子进程中调用它?如果我在映射函数中调用它,它将在每次函数执行时都被调用,而不是在进程fork时执行一次。
import multiprocessing as mp
NICENESS = 19
DATA = range(100000)
def foo(bar):
return bar * 2
pool = mp.Pool(100)
# 在这里以某种方式设置每个进程的niceness为NICENESS
pool.map(foo, DATA)
英文:
How can I set the niceness for each process in a multiprocessing.Pool
? I understand that I can increment niceness with os.nice()
, but how do call it in the child process after creating the pool? If I call it in the mapped function it will be called every time the function executes, rather than once when the process is forked.
import multiprocessing as mp
NICENESS = 19
DATA = range(100000)
def foo(bar):
return bar * 2
pool = mp.Pool(100)
# Somehow set niceness of each process to NICENESS
pool.map(foo, DATA)
答案1
得分: 6
# Import required libraries
import multiprocessing as mp
import os
# Define constants
NICENESS = 3
DATA = range(6)
# Worker function
def foo(bar):
return bar * 2
# Initializer function to set niceness value
def set_niceness(val):
os.nice(val)
if __name__ == '__main__':
# Initialize multiprocessing pool with the initializer
pool = mp.Pool(3, initializer=set_niceness, initargs=(NICENESS,))
# Map the worker function to the data using the pool
pool.map(foo, DATA)
Note: The provided code involves using multiprocessing in Python to set the niceness value of processes. The code initializes a multiprocessing pool with a specified niceness value using the initializer. The worker function is then mapped to the data using the pool, and the worker function doubles the input values. The output of the code is not included in this translation.
英文:
What about using an initializer for that? https://docs.python.org/3.8/library/multiprocessing.html#multiprocessing.pool.Pool
The function is called once when the pool is started so the os.nice() call in the initializer sets the niceness for the proces after that.
I've added some additional statements to show that it works in your worker function but the os.nice() calls should obviously be removed since you want a static niceness value.
import multiprocessing as mp
import os
NICENESS = 3
DATA = range(6)
def foo(bar):
newniceness = os.nice(1) # remove this
print('Additional niceness:', newniceness) # remove this
return bar * 2
def set_nicesness(val): # the initializer
newval = os.nice(val) # starts at 0 and returns newvalue
print('niceness value:', newval)
pool = mp.Pool(3, initializer=set_nicesness, initargs=(NICENESS,))
# Somehow set niceness of each process to NICENESS
pool.map(foo, DATA)
As you can see from the prints the niceness now starts at 3 (I've set this for NICENESS) and starts incrementing from there.
Or as a useable snippet
import multiprocessing as mp
import os
NICENESS = 3
def mp_function(bar: int) -> int:
return bar * 2
if __name__ == '__main__':
pool = mp.Pool(3, initializer=os.nice, initargs=(NICENESS,))
data = range(6)
pool.map(mp_function, data)
答案2
得分: 0
You can access the worker processes via pool._pool
.
With this you can probably set the nicesness of each worker individually.
import time
import psutil
import multiprocessing as mp
NICENESS =19
DATA = range(15)
def foo(bar):
time.sleep(bar)
return bar*2
if __name__=='__main__':
pool = mp.Pool(8) # 100 might not make sense if you only have 8 cores
processes =
for pid in processes:
p = psutil.Process(pid)
p.nice(NICENESS) # POSIX
# use this for Windows:
# p.nice(psutil.HIGH_PRIORITY_CLASS)
pool.map(foo, DATA)
I cannot test it on Linux, as I'm on Windows, but here it works well. Let me know if it works on Linux. Might be that you need to run the parent process as sudo
, as there's something with not being able to elevate other processes.
英文:
You can access the worker processes via pool._pool
.
With this you can probably set the nicesness of each worker individually.
import time
import psutil
import multiprocessing as mp
NICENESS =19
DATA = range(15)
def foo(bar):
time.sleep(bar)
return bar*2
if __name__=='__main__':
pool = mp.Pool(8) # 100 might not make sense if you only have 8 cores
processes =
for pid in processes:
p = psutil.Process(pid)
p.nice(NICENESS) # POSIX
# use this for Windows:
# p.nice(psutil.HIGH_PRIORITY_CLASS)
pool.map(foo, DATA)
I cannot test it on Linux, as I'm on Windows, but here it works well. Let me know if it works on Linux. Might be that you need to run the parent process as sudo
, as there's something with not being able to elevate other processes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论