我试图创建几个进程并通过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
答案 0 :(得分:2)
无法确定使用此库运行的进程的CPU名称,但您可以确定与进程相关的CPU核心编号。
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行为......