Is is possible to separate CPU resources between subprocess and main process in Python?
Question:
Suppose I have Python program with a main process A, it would have a main thread a, and invoke some threads a1, a2. And I spawn a subprocess from A, let’s say it B, and have the main thread b.
Is it possible to separate the CPU resources(CPU cores) between b and (a, a1, a2), that their CPU usages would not compete with each other?
Update(Answer): We need not regard the subprocess/main process. Just follow the accepted answer, e.g. set A cpu-affinity to some cores, B may inherit from this setting, but just setting B cpu-affinity to some other cores after B is spawned could achieve the expected behavior.
Answers:
You can use the psutil
library which implements the taskset command. That command allows you to set a process’ affinity for some CPUs. For example:
p = psutil.Process(pid)
p.cpu_affinity(cpus)
where cpus is a list of integers specifying the new CPUs affinity. The full documentation is here.
Suppose I have Python program with a main process A, it would have a main thread a, and invoke some threads a1, a2. And I spawn a subprocess from A, let’s say it B, and have the main thread b.
Is it possible to separate the CPU resources(CPU cores) between b and (a, a1, a2), that their CPU usages would not compete with each other?
Update(Answer): We need not regard the subprocess/main process. Just follow the accepted answer, e.g. set A cpu-affinity to some cores, B may inherit from this setting, but just setting B cpu-affinity to some other cores after B is spawned could achieve the expected behavior.
You can use the psutil
library which implements the taskset command. That command allows you to set a process’ affinity for some CPUs. For example:
p = psutil.Process(pid)
p.cpu_affinity(cpus)
where cpus is a list of integers specifying the new CPUs affinity. The full documentation is here.