-
Notifications
You must be signed in to change notification settings - Fork 1
/
go.py
48 lines (38 loc) · 1022 Bytes
/
go.py
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
import psutil
import threading
from multiprocessing import Process
stop=False
def busy_func():
while not stop:
pass
def print_cpu_usage(n, msg):
print msg
for i in range(n):
print psutil.cpu_percent(interval=1, percpu=True)
processes = []
def start_paralel(t):
print "starting", t
threads = []
for i in range(8):
if t == 'threads':
thr = threading.Thread(target=busy_func)
threads.append(thr)
thr.start()
else:
p = Process(target=busy_func)
processes.append(p)
p.start()
print "done"
return threads
print_cpu_usage(10, "before starting threads (idle)")
threads = start_paralel('threads')
print_cpu_usage(10, "threads running")
stop = True
for thr in threads:
thr.join()
print_cpu_usage(10, "after threads stopped (idle)")
stop = False
start_paralel('processes')
print_cpu_usage(10, "processes running")
for i in range(len(processes)):
processes[i].terminate()