-
Notifications
You must be signed in to change notification settings - Fork 0
/
Giang_Wk12_Ex.py
120 lines (88 loc) · 3.07 KB
/
Giang_Wk12_Ex.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Alex Giang
# Professor Abolghasemi
# CIS 502
# 11 March 2024
# Week 12, Exercise
# This file is intended to demonstrate beginning knowledge in implementing
# multithreading to run different functions at once.
import threading
import os
import time
def print_square(num):
time.sleep(0.18)
print(num ** 2)
def print_cube(num):
time.sleep(0.19)
print(num ** 3)
def task1():
print("task1 | Process ID:", os.getpid())
print("task1 | Thread Name:", threading.current_thread().name)
print("Time to sleep: 400ms")
time.sleep(0.01)
time.sleep(0.39)
print("Done sleeping... printing square of 5 in 180ms")
print_square(5)
def task2():
print("task2 | Process ID:", os.getpid())
print("task2 | Thread Name:", threading.current_thread().name)
print("Time to sleep: 700ms")
time.sleep(0.01)
time.sleep(0.69)
print("Done sleeping... printing cube of 5 in 190ms")
print_cube(5)
def mytimer():
print("mytimer | Process ID:", os.getpid())
print("mytimer | Thread Name:", threading.current_thread().name)
for i in range(0,2000,100):
print('\t', i, 'ms into execution...')
time.sleep(0.1)
if __name__ == "__main__":
print("__main__ | Process ID:", os.getpid())
print("__main__ | Thread Name:", threading.current_thread().name)
print("Main sleeps for an addt'l 300ms")
print("Iteration 1: Targetting task1 and task2")
t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')
ttime = threading.Thread(target=mytimer, name='ttime')
t1.start()
t2.start()
ttime.start()
t1.join()
t2.join()
ttime.join()
print("All other threads done...addt'l sleeping")
time.sleep(0.3)
print("Done with iteration 1!\n\n")
print("Iteration 2: Targetting print_square and print_cube with num=7")
print("Note: only short sleep statements in the print functions!")
print("Main sleeps for an addt'l 300ms")
t1 = threading.Thread(target=print_square, name='t1', args=(7,))
t2 = threading.Thread(target=print_cube, name='t2', args=(7,))
ttime = threading.Thread(target=mytimer, name='ttime')
t1.start()
t2.start()
ttime.start()
t1.join()
t2.join()
ttime.join()
print("All other threads done...addt'l sleeping")
time.sleep(0.3)
print("Done with iteration 2!\n\n")
print("Iteration 3: First iteration but concurrently (no joins)")
print("Main sleeps for 1,500ms")
t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')
ttime = threading.Thread(target=mytimer, name='ttime')
t1.start()
t2.start()
ttime.start()
# t1.join()
# t2.join()
# ttime.join()
time.sleep(1.5)
print("Done with iteration 3!")
time.sleep(1)
print("t1 alive? : ", t1.is_alive())
print("t2 alive? : ", t2.is_alive())
print("ttime alive? : ", ttime.is_alive())
print("\nThat means tasks are done!")