首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python模块“亲和力”有什么问题?

python模块“亲和力”有什么问题?
EN

Stack Overflow用户
提问于 2015-10-22 04:37:05
回答 1查看 907关注 0票数 2

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

环境:

Ubuntu 14.04 LTS;Python 2.7;

当我用

代码语言:javascript
复制
from affinity import set_process_affinity_mask, get_process_affinity_mask
import os
get_process_affinity_mask(os.getpid())

它抛出:

代码语言:javascript
复制
ValueError: (22, 'Invalid argument')

但手术结束了。但是,下面的_affinity.c源代码:

代码语言:javascript
复制
/* 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内核上工作?

EN

回答 1

Stack Overflow用户

发布于 2015-10-22 05:06:35

来自sched_getaffinity(2)手册页:

偏误 ..。 EINVAL (sched_getaffinity()和在2.6.9之前的内核中,sched_setaffinity()) cpusetsize小于内核使用的亲和掩码的大小。

考虑使用cpu_set_t作为掩码,使用size_t表示长度,而不是猜测它的长度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33273483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档