gpt4 book ai didi

python - python 模块 'affinity' 有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:33 24 4
gpt4 key购买 nike

我尝试在 python 中的不同 CPU 下运行进程,并找到了'affinity'模块,但它不起作用。

环境:

Ubuntu 14.04 LTS;Python 2.7;

当我使用

from affinity import set_process_affinity_mask, get_process_affinity_mask
import os
get_process_affinity_mask(os.getpid())

它抛出:

ValueError: (22, 'Invalid argument')

但是程序完成了。但是,下面的 _affinity.c 源代码:

/* Enfold Enterprise Server */
/* Copyright(C), 2004-5, Enfold Systems, LLC - ALL RIGHTS RESERVED */

/* Enfold Systems, LLC */
/* 4617 Montrose Blvd., Suite C215 */
/* Houston, Texas 77006 USA */
/* p. +1 713.942.2377 | f. +1 832.201.8856 */
/* www.enfoldsystems.com */
/* info@enfoldsystems.com */

/* Inspired by: */
/* http://www.linuxjournal.com/article/6799 */

#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
init_affinity(void)
{
Py_InitModule3("_affinity", methods, _affinity__doc__);
}

问题出在哪里?如何让进程以其他方式在选定的 CPU 内核上运行?

最佳答案

来自 sched_getaffinity(2) 手册页:

ERRORS

...

EINVAL

(sched_getaffinity() and, in kernels before 2.6.9, sched_setaffinity()) cpusetsize is smaller than the size of the affinity mask used by the kernel.

考虑使用 cpu_set_t 作为掩码,使用 size_t 作为长度,而不是猜测它可能有多长。

关于python - python 模块 'affinity' 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33273483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com