Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';python模块';亲和力';?_Python_C_Process_Affinity - Fatal编程技术网

什么';python模块';亲和力';?

什么';python模块';亲和力';?,python,c,process,affinity,Python,C,Process,Affinity,我尝试在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')

我尝试在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__);
}
/*包含企业服务器*/
/*版权所有(C),2004-5,Enfold Systems,LLC-保留所有权利*/
/*Enfold系统有限责任公司*/
/*蒙特罗斯大道4617号C215室*/
/*美国德克萨斯州休斯顿77006*/
/*p+1713.942.2377 | f+1 832.201.8856 */
/*www.enfoldsystems.com*/
/* info@enfoldsystems.com */
/*灵感来自:*/
/* http://www.linuxjournal.com/article/6799 */
#包括“Python.h”
#包括
PyDoc_STRVAR(_affinity__doc__,“Linux处理器affinity\n”);
静态PyObject*
获取进程关联掩码(PyObject*self,PyObject*args)
{
无符号长电流屏蔽;
无符号整数len=sizeof(cur_mask);
pid_t pid;
if(!PyArg\u ParseTuple(args,“i:get\u process\u affinity\u mask”、&pid))
返回NULL;
如果(附表1,第1页),
(cpu设置和电流掩码)<0){
PyErr_setfromrno(PyExc_ValueError);
返回NULL;
}
返回Py_BuildValue(“l”,cur_mask);
}
静态PyObject*
设置\u进程\u关联\u掩码(PyObject*self,PyObject*args)
{
未签名的长新_掩码;
无符号长电流屏蔽;
无符号整数len=sizeof(新掩码);
pid_t pid;
if(!PyArg_ParseTuple(args,“il:set_process_affinity_mask”、&pid、&new_mask))
返回NULL;
如果(附表1,第1页),
(cpu设置和电流掩码)<0){
PyErr_setfromrno(PyExc_ValueError);
返回NULL;
}
if(sched_setaffinity(pid,len,(cpu_set_t*)和new_mask)){
PyErr_setfromrno(PyExc_ValueError);
返回NULL;
}
返回Py_BuildValue(“l”,cur_mask);
}
静态PyMethodDef方法[]={
{“get_process_affinity_mask”,get_process_affinity_mask,METH_VARARGS,
“获取\u进程\u相关性\u掩码(pid)->\n\
获取“pid”的进程关联掩码。\n\n\
您可以获取任何正在运行的进程的关联掩码\n\
在系统中,即使您不是进程所有者。“},
{“set_process_affinity_mask”,set_process_affinity_mask,METH_VARARGS,
“设置\u进程\u关联\u掩码(pid,关联\u掩码)->\n\
将“pid”的进程关联掩码设置为“关联掩码”\n\
并返回上一个关联掩码。\n\n\
如果PID设置为零,则使用当前任务的PID。\n\n\
注意:您必须是中的“root”或“pid”的所有者\n\
为了能称之为“},
{NULL,NULL},
};
PyMODINIT_FUNC
初始关联(无效)
{
Py_InitModule3(“_affinity”,方法,_affinity_uuudoc_uuu);
}

问题发生在哪里?如何使进程以其他方式在选定的CPU内核上工作?

来自
sched\u getaffinity(2)
手册页:

错误

EINVAL

sched_getaffinity()
并且,在2.6.9之前的内核中,
sched_setaffinity()
)cpusetsize小于 内核使用的关联掩码

考虑对掩码使用
cpu\u set\u t
,长度使用
size\u t
,而不是猜测它可能有多长