我在多处理设置中使用Redis作为生产者/消费者关系的队列。
我的问题是我的制作人正在超载我的消费者,然后窃取它的CPU。
我的问题是,我可以在此设置中将整个处理器分配给特定的功能/进程(IE:消费者)。
答案 0 :(得分:1)
这不是Python开箱即用的东西。也有点特定于操作系统。有关在Linux下执行的操作,请参阅此答案:https://stackoverflow.com/a/9079117/4822566
答案 1 :(得分:0)
我在项目中遇到了类似的问题,其中3个服务器/调度程序各自在自己的CPU内核上运行,产生了CPU密集型工作人员-同时,这些工作人员被设计为使用他们正在运行的特定内核发挥最大潜力。
我需要确保永远不会在运行服务器/调度程序的CPU内核上产生(或使用-一些工作人员也使用多处理程序)这样的工作进程,因为这会导致以后的操作出现问题。
我使用了Ioannis Filippidis's答案中的以下代码,它非常适合将任何进程限制到任何内核。在此示例中,我使用了多处理池,但是child
中的代码可在任何multiprocessing.Process
中使用。注意:它不适用于macOS。
import multiprocessing as mp
def child(worker: int) -> None:
import psutil
import time
p = psutil.Process()
print(f"Child #{worker}: {p}, affinity {p.cpu_affinity()}", flush=True)
time.sleep(1)
p.cpu_affinity([worker])
print(f"Child #{worker}: Set my affinity to {worker}, affinity now {p.cpu_affinity()}", flush=True)
time.sleep(1 + 3 * worker)
print(f"Child #{worker}: Starting CPU intensive task now for 4 seconds on {p.cpu_affinity()}...", flush=True)
t_end = time.perf_counter() + 4
while time.perf_counter() < t_end:
pass
print(f"Child #{worker}: Finished CPU intensive task on {p.cpu_affinity()}", flush=True)
def main() -> None:
with mp.Pool() as pool:
# noinspection PyProtectedMember
workers: int = pool._processes
print(f"Running pool with {workers} workers")
for i in range(workers):
pool.apply_async(child, (i,))
# Wait for children to finnish
pool.close()
pool.join()
pass
if __name__ == '__main__':
main()
控制台中的输出:
Running pool with 16 workers
Child #0: psutil.Process(pid=16168, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #1: psutil.Process(pid=20864, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #2: psutil.Process(pid=15748, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #4: psutil.Process(pid=20600, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #3: psutil.Process(pid=17900, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #5: psutil.Process(pid=3288, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #7: psutil.Process(pid=19308, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #6: psutil.Process(pid=9768, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #8: psutil.Process(pid=1988, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #9: psutil.Process(pid=13960, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #11: psutil.Process(pid=3068, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #10: psutil.Process(pid=9636, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #12: psutil.Process(pid=18608, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #13: psutil.Process(pid=14356, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #14: psutil.Process(pid=14636, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #15: psutil.Process(pid=17372, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #0: Set my affinity to 0, affinity now [0]
Child #1: Set my affinity to 1, affinity now [1]
Child #2: Set my affinity to 2, affinity now [2]
Child #4: Set my affinity to 4, affinity now [4]
Child #3: Set my affinity to 3, affinity now [3]
Child #5: Set my affinity to 5, affinity now [5]
Child #7: Set my affinity to 7, affinity now [7]
Child #6: Set my affinity to 6, affinity now [6]
Child #8: Set my affinity to 8, affinity now [8]
Child #9: Set my affinity to 9, affinity now [9]
Child #10: Set my affinity to 10, affinity now [10]
Child #11: Set my affinity to 11, affinity now [11]
Child #12: Set my affinity to 12, affinity now [12]
Child #13: Set my affinity to 13, affinity now [13]
Child #14: Set my affinity to 14, affinity now [14]
Child #15: Set my affinity to 15, affinity now [15]
Child #0: Starting CPU intensive task now for 4 seconds on [0]...
Child #1: Starting CPU intensive task now for 4 seconds on [1]...
Child #0: Finished CPU intensive task on [0]
Child #2: Starting CPU intensive task now for 4 seconds on [2]...
Child #1: Finished CPU intensive task on [1]
Child #3: Starting CPU intensive task now for 4 seconds on [3]...
Child #2: Finished CPU intensive task on [2]
Child #4: Starting CPU intensive task now for 4 seconds on [4]...
Child #3: Finished CPU intensive task on [3]
Child #5: Starting CPU intensive task now for 4 seconds on [5]...
Child #4: Finished CPU intensive task on [4]
Child #6: Starting CPU intensive task now for 4 seconds on [6]...
Child #5: Finished CPU intensive task on [5]
Child #7: Starting CPU intensive task now for 4 seconds on [7]...
Child #6: Finished CPU intensive task on [6]
Child #8: Starting CPU intensive task now for 4 seconds on [8]...
Child #7: Finished CPU intensive task on [7]
Child #9: Starting CPU intensive task now for 4 seconds on [9]...
Child #8: Finished CPU intensive task on [8]
Child #10: Starting CPU intensive task now for 4 seconds on [10]...
Child #9: Finished CPU intensive task on [9]
Child #11: Starting CPU intensive task now for 4 seconds on [11]...
Child #10: Finished CPU intensive task on [10]
Child #12: Starting CPU intensive task now for 4 seconds on [12]...
Child #11: Finished CPU intensive task on [11]
Child #13: Starting CPU intensive task now for 4 seconds on [13]...
Child #12: Finished CPU intensive task on [12]
Child #14: Starting CPU intensive task now for 4 seconds on [14]...
Child #13: Finished CPU intensive task on [13]
Child #15: Starting CPU intensive task now for 4 seconds on [15]...
Child #14: Finished CPU intensive task on [14]
Child #15: Finished CPU intensive task on [15]
Process finished with exit code 0