Python Multiprocessing

In python, is there a way to find out which processor the process is running on? For example, if I create different processes for different tasks using the multiprocessing module, is it possible to determine the kernel in which each process works?

+7
source share
3 answers

Short answer

No, It is Immpossible.

Long answer

in a general situation, you cannot do this, because processes are not tied to specific kernels. Processes do not have fixed processors, on which they are always guaranteed to work: the operating system depends to a certain extent, which kernel it uses to start a specific process at a certain time. This decision was made by scheduling , and its implementation is OS-specific.

On certain operating systems, you can control how processors are used to retrieve specific processes. Assigning preferred processors is often referred to as processor affinity . Even installing affinitity does not guarantee that the process will always run on the given kernels: ultimately, before the OS (and CPU), you need to decide how planning is done in the future.

Of all the operating systems that I know of, the closest I could think of would be Linux sched_getcpu , which can be used "to determine the processor the calling thread is running on" (see man sched_getcpu ). Even if you check the current processor using this function, the kernel can immediately change the kernel.

+3
source

Python may not have such a function now, but

C libraries are available, you can link them with python code if necessary.

Link: How do I know in which kernel the process is running?

+3
source

I do not think that this can be done reliably, since the process is not limited to the kernel. One process can run on one or more cores (if it uses threads), and the cores that are used to execute it can change over time, as os tries to balance the workload.

For a good way to get process related information, check out the psutil library.

+1
source

All Articles