Python multiprocessing: find core ID

匆匆过客 提交于 2020-12-12 18:16:30

问题


I am testing Python's multiprocessing module on a cluster with SLURM. I want to make absolutely sure that each of my tasks are actually running on separate cpu cores as I intend. Due to the many possibilities of configuring SLURM, this is not at all obvious.

Therefore, I was wondering if there is a way to get core specific information from the running Python task. I need my Python script to get information about the core it's running on which allows to distinguish between the various cores.

Consider the following script. There are 10 tasks launched. Is there a way for each of them to print core specific information about the core they have been assigned to?

import multiprocessing

def hello():
    print "Hello World"

pool = multiprocessing.Pool()
jobs = []
for j in range(10):
    p = multiprocessing.Process(target = hello)
    jobs.append(p)
    p.start()

The modules I found here and here give CPU specific information, but these are about available CPUs, not core information at runtime.


回答1:


On Linux, FreeBSD, SunOS you can use the psutil-module for that:

psutil.Process().cpu_num()

Note, that as long as you don't set the cpu-affinity as well, your OS will arbitrarily migrate the execution to different cores. Processes are not assigned to specific cores by default and it's usually best to let your OS decide on which core to run something.



来源:https://stackoverflow.com/questions/56430808/python-multiprocessing-find-core-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!