-
Notifications
You must be signed in to change notification settings - Fork 26
/
threads_part2_video.py
68 lines (31 loc) · 1.05 KB
/
threads_part2_video.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Threading part 2
#multiple threads
import threading
import time
def sleeper(n, name):
print('Hi, I am {}. Going to sleep for 5 seconds \n'.format(name))
time.sleep(n)
print('{} has woken up from sleep \n'.format(name))
start = time.time()
threads = []
for i in range(5):
t = threading.Thread(target = sleeper, name = 'thread{}'.format(i), args =(5,'thread{}'.format(i) ) )
threads.append(t)
t.start()
print('{} has started \n'.format(t.name))
for i in threads:
i.join()
end = time.time()
print('time is {}'.format(end - start))
#example 2 without threads
import time
def sleeper(n, i):
print ('Hi, I am function {}. Going to sleep for 5 seconds \n'.format(i))
time.sleep(n)
print('function{} has woken up from sleep \n'.format(i))
start = time.time()
for i in range(5):
print('{} has started \n'.format(i))
x = sleeper(5, i)
end = time.time()
print('time taken: {}'.format(end - start))