7

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

4
  • 1
    Programmatically or through the OS? If OS, then which OS? Commented Nov 29, 2011 at 7:52
  • What do you mean by programmatically or OS? If you mean through the program or from outside it, I want it from inside the program. For example I can get the process ID with os.getpid(), can I do something similar to find the CPU which that process is running on. Commented Nov 29, 2011 at 8:00
  • Btw. why would you want to find out the CPU number in the first place? What are you trying to achieve? Commented Nov 29, 2011 at 8:51
  • I want to do some processing on the packets coming in on my network card, and I want to divide the process for each packet on the different cores. I want to check how many of the processes are being processed by each CPU. Commented Nov 29, 2011 at 9:57

2 Answers 2

3

Short answer

No, it is not possible.

Long answer

in a general situation you cannot do that since processes are not bound to specific cores. Processes do not have fixed CPUs that they are always guaranteed to run on: it is up to the operating system to decide, which core it uses for running a specific process on a specific time. This decision making is called scheduling and its implementation is OS specific.

On specific operating systems, you may be able to control, how processors are used for excution of specific processes. The assignment of preferred processors is often referred to as processor affinity. Even setting affinitity does not guarantee that a process will always be executed on given cores: it is ultimately up to the OS (and CPU) to decide how the scheduling is ultimately executed.

From all OSes I know, the closest thing I could think of would be Linux's sched_getcpu which can be used "to determine the CPU on which the calling thread is running" (see man sched_getcpu). Even given that you check current CPU with this function, the kernel may change the core right after.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't know if there exists a cross-platform solution to handle setting and querying the process affinity. On Windows you can set the process affinity to a particular set of cores by calling SetProcessAffinityMask with a bit vector.
I don't think it exists either. From PyPi I found affinity 0.1.0 (pypi.python.org/pypi/affinity), which states to provide wrappers for affinity functions on Windows and some specific Linux kernels. I haven't tried it though and even that library wouldn't provide complete solution to the given problem, which in itself is based on the misconception that processes are bound to specific CPus.
I don't see the reason why processes not being bound to specific CPUs doesn't allow us to retrieve which CPU they're being run on at specific time intervals. Why can't I check which CPU is running my process for example every 10 seconds?
Because it may change immediately after that instruction.
1

I don't think this can be done reliably as a process is not limited to a core. One process can execute on one or more cores (if it uses threads) and the cores that are used to execute it can change over time as the os tries to balance the workload.

As for a nice way to get process-related information look in to the psutil library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.