Python Glossary Русский

Python 3.3+Linuxosfunction

os.sched_setaffinity

os — scheduler (sched)

Set the CPUs a process is allowed to run on (CPU affinity).

Syntax

os.sched_setaffinity(...)

It pins a process to the given CPUs — useful in benchmarks so results do not jitter as the process migrates between cores. Threads created later inherit the affinity. Narrowing the set needs no special rights; changing another process requires permission over it.

Examples

import os
print(os.sched_getaffinity(0))  # → ? только Unix: множество ядер, доступных текущему процессу (pid=0), например {0, 1, 2, 3}
os.sched_setaffinity(0, {0})  # → ? только Unix: процесс закреплён за ядром 0
print(len(os.sched_getaffinity(0)))  # → ? только Unix: 1 — честное число доступных ядер (точнее, чем os.cpu_count())
os.sched_setaffinity(0, {0, 1})  # → ? только Unix: вернули процессу два ядра
os.sched_setaffinity(0, set())  # → OSError: [Errno 22] Invalid argument — пустая маска запрещена

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy