-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpsutil_process_cpu_affinity.py
More file actions
44 lines (33 loc) · 1011 Bytes
/
Copy pathpsutil_process_cpu_affinity.py
File metadata and controls
44 lines (33 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import logging
import sys
import threading
import time
import psutil
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
if __name__ == "__main__":
logging.info("We start a sub thread first")
def worker():
i = 0
while True:
i += 1
if i % 10000000 == 0:
logging.info("switch...")
time.sleep(0.01)
new_thread = threading.Thread(
target=worker,
)
new_thread.daemon = True
new_thread.start()
cpu_count = psutil.cpu_count()
logging.info("cpu_count = %s", cpu_count)
p = psutil.Process()
logging.info("cpu_affinity = %s", p.cpu_affinity())
time.sleep(10)
p.cpu_affinity([0, 1])
logging.info("process will now only run on CPU 0 and 1")
logging.info("cpu_affinity = %s", p.cpu_affinity())
time.sleep(10)
p.cpu_affinity([])
logging.info("process will now run all eligible CPUs")
logging.info("cpu_affinity = %s", p.cpu_affinity())
time.sleep(10)