-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpsutil_process_num_threads.py
More file actions
59 lines (47 loc) · 1.44 KB
/
psutil_process_num_threads.py
File metadata and controls
59 lines (47 loc) · 1.44 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import logging
import sys
import threading
import time
import psutil
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
if __name__ == "__main__":
p = psutil.Process()
logging.info("Before starting new thread: threads number = %s", p.num_threads())
for thread in p.threads():
logging.info(
"pid = %s, id = %s, user_time = %s, system_time = %s",
p.pid,
thread.id,
thread.user_time,
thread.system_time,
)
def worker():
time.sleep(3)
new_thread = threading.Thread(target=worker)
new_thread.daemon = True
new_thread.start()
logging.info("After starting new thread: threads number = %s", p.num_threads())
for thread in p.threads():
logging.info(
"pid = %s, id = %s, user_time = %s, system_time = %s",
p.pid,
thread.id,
thread.user_time,
thread.system_time,
)
new_thread.join()
logging.info("After new thread exiting: threads number = %s", p.num_threads())
for thread in p.threads():
logging.info(
"pid = %s, id = %s, user_time = %s, system_time = %s",
p.pid,
thread.id,
thread.user_time,
thread.system_time,
)
time.sleep(1)
logging.info(
"After new thread exiting and sleep 1 seconds: threads number = %s",
p.num_threads(),
)
logging.info("Done")