Python os.sched_getaffinity()
Python中的os.sched_getaffinity()方法用于获取具有指定进程id的进程可以在其上运行的一组cpu。
注意:此方法仅在某些UNIX平台上可用。
语法:os.sched_getaffinity(pid)
参数:
pid:需要设置CPU亲和性掩码的进程id。进程的CPU亲和性掩码决定它可以在哪些CPU上运行。
pid为0表示调用进程。
返回类型:该方法返回一个集合对象,该对象表示cpu数量,指定进程id的进程可以在其上运行。
示例1
使用os.sched_getaffinity()方法
# Python program to explain os.sched_getaffinity() method # importing os module import os # Get the set of CPUs # on which the calling process # is eligible to run. # 0 as pid represents the # calling process pid = 0 affinity = os.sched_getaffinity(pid) # Print the result print(affinity) # Change the CPU affinity mask # of the calling process # using os.sched_setaffinity() method # Below CPU affinity mask will # restrict a process to only # these 2 CPUs (0, 1) i.e process can # run on these CPUs only affinity_mask = {0, 1} pid = 0 os.sched_setaffinity(0, affinity_mask) # Now again, Get the set of CPUs # on which the calling process # is eligible to run. pid = 0 affinity = os.sched_getaffinity(pid) # Print the result print(affinity)
输出:
{0, 1, 2, 3} {0, 1}
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com