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?

s_hamed
  • 71
  • 2
  • 1
    Programmatically or through the OS? If OS, then which OS? – jzila Nov 29 '11 at 07: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. – s_hamed Nov 29 '11 at 08:00
  • Btw. why would you want to find out the CPU number in the first place? What are you trying to achieve? – jsalonen Nov 29 '11 at 08: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. – s_hamed Nov 29 '11 at 09:57

2 Answers2

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.

jsalonen
  • 26,663
  • 14
  • 82
  • 104
  • 1
    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](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686223%28v=vs.85%29.aspx) with a bit vector. – Eryk Sun Nov 29 '11 at 08:10
  • I don't think it exists either. From PyPi I found `affinity 0.1.0` (http://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. – jsalonen Nov 29 '11 at 08:17
  • 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? – s_hamed Nov 29 '11 at 08:24
  • 1
    Because it may change immediately after that instruction. –  Nov 29 '11 at 08:32
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.

Toni Ruža
  • 7,174
  • 2
  • 26
  • 30