Python affinity.get\u进程\u affinity\u掩码(pid)返回值错误22
我想把我的线程工作者放在一个特定的CPUi上,想测试GIL如何影响我的程序…,我找到了一个名为的第三方库。 我使用pip install affinity使其在我的VMLinux中可用,不幸的是,我得到以下错误:Python affinity.get\u进程\u affinity\u掩码(pid)返回值错误22,python,multithreading,cpu,affinity,Python,Multithreading,Cpu,Affinity,我想把我的线程工作者放在一个特定的CPUi上,想测试GIL如何影响我的程序…,我找到了一个名为的第三方库。 我使用pip install affinity使其在我的VMLinux中可用,不幸的是,我得到以下错误: >>>pid = os.getpid() >>>affinity.get_process_affinity_mask(pid) Traceback (most recent call last): File "<stdin>", line
>>>pid = os.getpid()
>>>affinity.get_process_affinity_mask(pid)
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: (22, 'Invalid argument')
有人能给我一些关于这个错误的线索吗?或者在我的案例中还有其他方法可以使用吗?看看C代码和sched_getaffinity的手册页,我并不太惊讶它可能会因为“无效参数”而失败。C代码传入一个无符号long,函数需要一个cpu集合,它最多可以是128个无符号long 我对cpu\u set\t结构了解不多,但表面上看,每个物理cpu可能有一个值,单个核表示为其中一个值内的位。在这种情况下,我希望这个模块在任何有多个CPU的机器上都会出现故障 我的系统是单CPU双核的,所以这个模块适合我。您的虚拟机是否配置了多个物理CPU?作为一项测试,尝试重新配置它,使其只有一个具有多个内核,然后看看您是否更成功 如果我是对的,解决这个问题的唯一方法是修改C模块以正确处理cpu_set_t结果,可能使用cpu_set 3中描述的宏 您的虚拟机环境是什么?虚拟机软件,CPU/核心计数,Linux版本? 尝试此测试程序,看看您的输出是否有任何不同:
$ cat test.c
#include <stdio.h>
// The CPU_SET man page says to use the second definition, but my system
// wouldn't compile this code without the first one.
#define __USE_GNU
#define _GNU_SOURCE
#include <sched.h>
#include <errno.h>
int main(void) {
cpu_set_t cur_mask;
unsigned int len = sizeof(cpu_set_t);
if (sched_getaffinity(0, len, &cur_mask) < 0) {
printf("Error: %d\n", errno);
return errno;
}
int cpu_count = CPU_COUNT(&cur_mask);
printf("Cpu count: %d and size needed: %d\n", cpu_count, CPU_ALLOC_SIZE(cpu_count));
return 0;
}
$ gcc -std=c99 test.c
$ ./a.out
Cpu count: 2 and size needed: 8
在我的系统上,一个未签名的长度似乎足以容纳64个CPU,所以它看起来比我想象的要简单得多。不同的硬件/体系结构/内核版本可能总是不同的。谢谢@ValdarMoridin,不幸的是,当我在只有1个cpui的VM上测试时,我仍然有相同的错误。我从VM设置以及使用multiprocessing.cpu\u count找到了这些信息。
$ cat test.c
#include <stdio.h>
// The CPU_SET man page says to use the second definition, but my system
// wouldn't compile this code without the first one.
#define __USE_GNU
#define _GNU_SOURCE
#include <sched.h>
#include <errno.h>
int main(void) {
cpu_set_t cur_mask;
unsigned int len = sizeof(cpu_set_t);
if (sched_getaffinity(0, len, &cur_mask) < 0) {
printf("Error: %d\n", errno);
return errno;
}
int cpu_count = CPU_COUNT(&cur_mask);
printf("Cpu count: %d and size needed: %d\n", cpu_count, CPU_ALLOC_SIZE(cpu_count));
return 0;
}
$ gcc -std=c99 test.c
$ ./a.out
Cpu count: 2 and size needed: 8