Search code examples
pythonmultiprocessingpython-multiprocessing

How can a Python program determine which core it is running on?


I need to debug a Python 3 program that uses the multiprocessing module.

I want to keep track of which cores (of a multi-core machine) are getting used and how.

Q: I am looking for a way for the Python code to determine which core is running it.


The closest I have found is to use the following:

multiprocessing.current_process()._identity[0] - 1

Putting aside the fact that such code appears to be "going behind the API,"1 as far as I can tell, the code that initializes the _identity attribute makes no reference to the underlying hardware2, which I think is unsatisfactory.


1 For one thing, I can find no official documentation for the _identity attribute, as one would expect from the leading underscore in the name.
2 More specifically, this code evaluates something like next(_process_counter), where _process_counter is initially set to the value itertools.count(1), and uses the the result as the basis for the _identity attribute's value.


Solution

  • This is absolutely going to be an OS-specific problem, but here's how to do it in Windows (source). As mentioned in the comments, the processor number will likely be changing all the time unless you specifically pin the task to a given core (outside the scope of this answer).

    from ctypes import *
    from ctypes import wintypes
    
    class PROCESSOR_NUMBER(Structure):
        _fields_ = [("Group", wintypes.WORD),
                    ("Number", wintypes.BYTE),
                    ("Reserved", wintypes.BYTE)]
    
    pn = PROCESSOR_NUMBER()
    
    windll.kernel32.GetCurrentProcessorNumberEx(byref(pn))
    
    print(pn.Number)
    

    Edit: for Ubuntu 20.04.5 (WSL)

    from ctypes import *
    libc = CDLL("libc.so.6")
    print(libc.sched_getcpu())