版权声明:本文为CSDN博主「人间再无张居正」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013887652/article/details/128663484
========================================
import os
from multiprocessing import Process
cpu_available = os.sched_getaffinity(os.getpid()) # 获取可被绑定的核心
os.sched_setaffinity(os.getpid(), list(cpu_available)[:1]) # 绑定核心
def func():
while True:
pass
process = [Process(target=func) for i in range(4)]
for proc in process:
proc.start()
上述代码,开了4个进程,但是只绑定了一个核,此时,查看CPU的利用率,可以看到,当进程只绑定一个核时,对于CPU的使用率只有100%左右
修改成绑定两个核心
import os
from multiprocessing import Process
cpu_avia = os.sched_getaffinity(os.getpid())
os.sched_setaffinity(os.getpid(), list(cpu_avia)[:2]) # 绑定两个核心
def func():
while True:
pass
process = [Process(target=func) for i in range(4)]
for proc in process:
proc.start()
此时再查看CPU的使用率,差不多200%,即占用到了两个核的资源
=======================================
标签:sched,python,绑定,process,import,os,CPU From: https://www.cnblogs.com/devilmaycry812839668/p/17066212.html