Pythonista 達の熱き闘いが,
今,始まる...!!
[2010/12/12 22:32:15] 登録
■ 名前
■ ステータス
HP | SP | 攻撃力 | 集中力 | 防御力 | 素早さ | 運 |
---|---|---|---|---|---|---|
170 | 31 | 57 | 18 | 4 | 5 | 17 |
■ 必殺技
名前 | タイプ | レベル | 消費 SP |
---|---|---|---|
スマッシュボンバー | SingleAttackType | 2 | 12 |
凄妙弾烈 | RangeAttackType | 3 | 17 |
ラリアットドロップ | MultiAttackType | 2 | 12 |
■ コード
#!/usr/bin/env python """interface to sched_get/setaffinity(2) """ import os import sys import ctypes if sys.platform != 'linux2': raise OSError _libc = ctypes.CDLL('libc.so.6') _sched_setaffinity = _libc.sched_setaffinity _sched_getaffinity = _libc.sched_getaffinity _sched_setaffinity.argtypes = [ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong)] _sched_getaffinity.argtypes = [ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong)] def getcpunum(): """get number of cpus """ f = open('/proc/cpuinfo') num = 0 for line in f: if line[:9] == 'processor': num += 1 f.close() return num def cput2dict(val): """translate cpu_set_t to dictionary """ num = getcpunum() _dict = {} for n in range(num): _dict[n] = bool(val & (1 << n)) return _dict def dict2cput(_dict): """translate dictionary to cpu_set_t """ num = getcpunum() cput = 0 for n in range(num): if _dict.get(n, False): cput |= (1 << n) return cput def get_affinity(pid): """do sched_getaffinity(2) to pid """ mask = ctypes.c_ulong(0) c_ulong_size = ctypes.sizeof(ctypes.c_ulong) if _sched_getaffinity(pid, c_ulong_size, mask) < 0: raise OSError return cput2dict(mask.value) def set_affinity(pid, _dict): """do sched_setaffinity(2) to pid with _dict """ mask = ctypes.c_ulong(dict2cput(_dict)) c_ulong_size = ctypes.sizeof(ctypes.c_ulong) if _sched_setaffinity(pid, c_ulong_size, mask) < 0: raise OSError return None if __name__ == '__main__': print get_affinity(os.getpid()) set_affinity(os.getpid(), {1: True, 2: True}) print get_affinity(os.getpid())