1

我正在尝试创建几个进程并通过 Python 多处理模块运行它们,我使用下面的代码来确定正在提交的每个进程的进程名称,但我注意到所有进程都在同一个处理器上运行,至少名称看起来相同.

作业何时开始在其他处理器上运行?如何识别其运行的处理器名称?

from multiprocessing import Process
import platform

def process1():
    print 'process_1 processor =',platform.processor() 

def process2():
    print 'process_2 processor =',platform.processor()

def main():
    print 'Main processor =',platform.processor()
    processlist = []
    p1 = Process(target = process1)
    p1.start()
    processlist.append(p1)

    p2 = Process(target = process2)
    p2.start()
    processlist.append(p2)

    for i in processlist:
        i.join()

if __name__ == '__main__':
    main()

OUtput
Main processor = Intel64 Family 6 Model 37 Stepping 2, GenuineIntel
process_2 processor = Intel64 Family 6 Model 37 Stepping 2, GenuineIntel
process_1 processor = Intel64 Family 6 Model 37 Stepping 2, GenuineIntel
4

1 回答 1

2

无法确定使用此库在哪个进程上运行的 CPU 名称,但您可以确定与进程相关的 CPU CORE 编号。

import psutil,os
self_process = psutil.Process(os.getpid())
self_process.get_cpu_affinity()
# [0,1] - i have 2 core cpu and process can load both cores
self_process.set_cpu_affinity([1])
# now this process can load only one core #1

您可以在主进程中获取 pid,并将 CPU 内核设置为在生成后运行。不知道多CPU行为...

于 2013-08-30T20:46:18.087 回答