forked from AnastasiyaMax/MLDS_python_course_fall21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWS8-threads-processes.py
More file actions
41 lines (33 loc) · 790 Bytes
/
WS8-threads-processes.py
File metadata and controls
41 lines (33 loc) · 790 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
from multiprocessing import freeze_support
from multiprocessing import Pool
from multiprocessing import Process
from threading import Thread
import time
def countdown(n):
while n > 0:
n -= 1
count = 50000000
start = time.time()
countdown(count)
print(time.time() - start)
t1 = Thread(target=countdown, args=(count//2,))
t2 = Thread(target=countdown, args=(count//2,))
start = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
print(time.time() - start)
freeze_support()
start = time.time()
with Pool(2) as p:
p.map(countdown, [count//2, count//2])
print(time.time() - start)
p1 = Process(target=countdown, args=(count//2,))
p2 = Process(target=countdown, args=(count//2,))
start = time.time()
p1.start()
p2.start()
p1.join()
p2.join()
print(time.time() - start)