如何在 Ubuntu 中全局限制 python 对内核的使用? [英] How to limit python's use of cores globally in Ubuntu?

查看:28
本文介绍了如何在 Ubuntu 中全局限制 python 对内核的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置任何 global 参数(最好是 env 变量)来限制 Python 使用的最大内核数?我知道有一些参数可以为特定的程序包设置,例如多线程或 numpy,但我想控制 Python 本身,以便我确定它不会使用超过 N 个内核.

Is there any global parameter (preferably env variable) one can set to limit the maximal cores used by Python? I know there are parameters to set for specific packages such as multithreading or numpy, but I would like to control Python itself so that I'm sure it won't use more than N cores.

我想要一个全局参数的原因是我可以将它设置给我网络中的用户,这样我们就可以在同一台机器上一起工作;目前,每当运行脚本时,Python 只会使用它所能使用的最多内核,而其他内核会被干扰.

The reason I would like a global parameter is so that I can set it to users in my network so that we would be able to work together on the same machine; currently whenever one runs a script, Python just uses the most cores it can, jamming the others.

Python 版本 2.6-7,Ubuntu 版本 14.04.

Python version 2.6-7, Ubuntu version 14.04.

谢谢.

推荐答案

使用 affinity您可以将操作系统级别上的每个进程限制为单核.affinity 只是调用底层 linux sched_setaffinity 函数,您可以将其设置为特定的核心编号或范围.

using the affinity package you can limit the each process on a os level to single core. affinity just calls the underlying linux sched_setaffinity function, you can either set it to a specific core number or to a range.

import affinity
import os
pid = os.getpid()
affinity.set_process_affinity_mask(pid, 1) # 1 is the first core
affinity.set_process_affinity_mask(pid, 2) # 2 is the second core

请参阅 pthread_setaffinity_np 手册页,了解有关如何设置内核范围的更多详细信息,请注意,如果您想设置其他进程的关联性,您应该具有管理该进程的权限.还要注意,子进程继承了父进程的亲缘关系.

see the pthread_setaffinity_np man page for more details on how to set ranges of cores, notice that if you want to set the affinity of other processes you should have permissions to manage that process. furthermore notice that children processes inherit the father's process affinity.

这篇关于如何在 Ubuntu 中全局限制 python 对内核的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆