#include "Python.h"
#include <sched.h>
PyDoc_STRVAR(_affinity__doc__, "Linux Processor Affinity\n");
static PyObject *
get_process_affinity_mask(PyObject *self, PyObject *args)
{
unsigned long cur_mask;
unsigned int len = sizeof(cur_mask);
pid_t pid;
if (!PyArg_ParseTuple(args, "i:get_process_affinity_mask", &pid))
return NULL;
if (sched_getaffinity(pid, len,
(cpu_set_t *)&cur_mask) < 0) {
PyErr_SetFromErrno(PyExc_ValueError);
return NULL;
}
return Py_BuildValue("l", cur_mask);
}
static PyObject *
set_process_affinity_mask(PyObject *self, PyObject *args)
{
unsigned long new_mask;
unsigned long cur_mask;
unsigned int len = sizeof(new_mask);
pid_t pid;
if (!PyArg_ParseTuple(args, "il:set_process_affinity_mask", &pid, &new_mask))
return NULL;
if (sched_getaffinity(pid, len,
(cpu_set_t *)&cur_mask) < 0) {
PyErr_SetFromErrno(PyExc_ValueError);
return NULL;
}
if (sched_setaffinity(pid, len, (cpu_set_t *)&new_mask)) {
PyErr_SetFromErrno(PyExc_ValueError);
return NULL;
}
return Py_BuildValue("l", cur_mask);
}
static PyMethodDef methods[] = {
{"get_process_affinity_mask", get_process_affinity_mask, METH_VARARGS,
"get_process_affinity_mask(pid) ->\n\
Get the process affinity mask of 'pid'.\n\n\
You can get the affinity mask of any process running\n\
in the system, even if you are not the process owner."},
{"set_process_affinity_mask", set_process_affinity_mask, METH_VARARGS,
"set_process_affinity_mask(pid, affinity_mask) ->\n\
Set the process affinity mask of 'pid' to 'affinity_mask'\n\
and return the previous affinity mask.\n\n\
If the PID is set to zero, the PID of the current task is used.\n\n\
Note: you must be 'root' or the owner of 'pid' in\n\
order to be able to call this."},
{NULL, NULL},
};
PyMODINIT_FUNC
initaffinity_linux(void)
{
Py_InitModule3("affinity_linux", methods, _affinity__doc__);
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Python的线程在多核情况下,由于GIL的存在,运行效率会比单核情况下还低,这个时候,可以通过设置运行Python代码的进程所在的CPU以提高线程的性能,达到和单核处理器时一致的性能。该代码从 https://pypi.python.org/pypi/affinity/0.1.0 修改而来,去掉了原代码中对pywin32的依赖。
资源推荐
资源详情
资源评论
收起资源包目录
affinity.rar (5个子文件)
Setup.py 869B
affinity_win32.pyx 2KB
Makefile 149B
affinity_linux.c 2KB
affinity.py 119B
共 5 条
- 1
资源评论
- leeezp2022-03-23不好啊!!
- PerkyRookie2018-07-19没有readme,不知道怎么运行
I2Cbus
- 粉丝: 217
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- TCP转COM 以太网 网口转串口 小工具C#源码
- %E3%80%90%E6%95%B0%E7%94%B5%E3%80%91%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A874LS112%EF%BC%88%E6%88%9674L.html
- 基于three.js + canvas 实现的爱心代码+播放器效果
- 基于java实现的的数独小游戏(java)
- Echarts-售货机大数据分析平台
- 手扶电梯地铁电梯缺陷检测数据集VOC+YOLO格式67张2类别.7z
- 串口调试助手工具C#源码多区轮询
- 基于 C 实现的环形数组(环形数组的实现)
- 阿牛依日0.zip
- 交通灯.smart
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功