Skip to content

Commit 1321693

Browse files
committed
Codes 4 Week07
1 parent b779ee4 commit 1321693

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

Week07/creating_thread.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import threading
2+
import time
3+
4+
5+
"""
6+
This file includes the following ways to create a thread:
7+
1. Create a thread from a function
8+
2. Create a thread from a class
9+
3. Create a thread from a function and pass arguments
10+
4. Create two threads and join them
11+
5. Create a daemon thread
12+
6. Combine all the above and give names to the threads
13+
"""
14+
15+
16+
# 1. Create a thread from a function
17+
def create_thread_from_function():
18+
def thread_1():
19+
print(
20+
f"This is a thread created from a function "
21+
f"with id {threading.get_native_id()} "
22+
)
23+
24+
t = threading.Thread(target=thread_1)
25+
t.start()
26+
27+
28+
# 2. Create a thread from a class
29+
def create_thread_from_class():
30+
class Thread2(threading.Thread):
31+
def __init__(self):
32+
super().__init__()
33+
34+
def run(self):
35+
print(
36+
f"This is a thread created from a class "
37+
f"with id {threading.get_native_id()}"
38+
)
39+
40+
t = Thread2()
41+
t.start()
42+
43+
44+
# 3. Create a thread from a function and pass arguments
45+
def create_thread_from_function_and_pass_arguments():
46+
def thread_3(s):
47+
print(
48+
f"This is a thread created from a function "
49+
f"with id {threading.get_native_id()} "
50+
f"and your argument is {s}"
51+
)
52+
53+
t = threading.Thread(target=thread_3, args=("Thread 3",))
54+
t.start()
55+
56+
57+
# 4. Create two threads and join them
58+
def create_two_threads_and_join_them():
59+
def thread_4_1():
60+
print(f"This is thread 4_1 with id {threading.get_native_id()}")
61+
time.sleep(1)
62+
print(f"Thread 4_1 is done")
63+
64+
def thread_4_2():
65+
print(f"This is thread 4_2 with id {threading.get_native_id()}")
66+
time.sleep(3)
67+
print(f"Thread 4_2 is done")
68+
69+
def thread_4_3():
70+
print(f"This is thread 4_3 with id {threading.get_native_id()}")
71+
print(f"I can only start after thread 4_1 and thread 4_2 are done")
72+
time.sleep(1)
73+
print(f"Thread 4_3 is done")
74+
75+
t1 = threading.Thread(target=thread_4_1)
76+
t2 = threading.Thread(target=thread_4_2)
77+
t3 = threading.Thread(target=thread_4_3)
78+
t1.start()
79+
t2.start()
80+
t1.join()
81+
t2.join()
82+
t3.start()
83+
84+
85+
# 5. Create a daemon thread
86+
def create_daemon_thread():
87+
def thread_5():
88+
print(f"This is a daemon thread with id {threading.get_native_id()}")
89+
while True:
90+
time.sleep(1)
91+
print(f"I am still alive because I am a daemon thread")
92+
print(f"I will never be printed because I am a daemon thread")
93+
94+
def thread_6():
95+
print(f"This is a non-daemon thread with id {threading.get_native_id()}")
96+
time.sleep(5)
97+
print(f"I am done therefore the program will exit")
98+
99+
t1 = threading.Thread(
100+
target=thread_5
101+
) # or t1 = threading.Thread(target=thread_5, daemon=True)
102+
t2 = threading.Thread(target=thread_6)
103+
t1.daemon = True
104+
t1.start()
105+
t2.start()
106+
107+
108+
# 6. Combine all the above and give names to the threads
109+
def combine_all_and_give_names_to_threads():
110+
def a_thread_to_join_1():
111+
print(f"This is thread 1 with id {threading.get_native_id()}")
112+
time.sleep(1)
113+
print(f"Thread 1 is done")
114+
115+
def a_thread_to_join_2():
116+
print(f"This is thread 2 with id {threading.get_native_id()}")
117+
time.sleep(3)
118+
print(f"Thread 2 is done")
119+
120+
def a_thread_after_join():
121+
print(f"This is thread 3 with id {threading.get_native_id()}")
122+
print(f"I can only start after thread 1 and thread 2 are done")
123+
time.sleep(1)
124+
print(f"Thread 3 is done")
125+
126+
def a_daemon_thread_running_in_background():
127+
print(f"This is a daemon thread with id {threading.get_native_id()}")
128+
while True:
129+
time.sleep(1)
130+
print(f"I am still alive because I am a daemon thread")
131+
132+
def a_non_daemon_thread():
133+
print(f"This is a non-daemon thread with id {threading.get_native_id()}")
134+
time.sleep(5)
135+
print(f"The program will exit when no non-daemon threads are left")
136+
137+
t1 = threading.Thread(target=a_thread_to_join_1, name="Thread 1")
138+
t2 = threading.Thread(target=a_thread_to_join_2, name="Thread 2")
139+
t3 = threading.Thread(target=a_thread_after_join, name="Thread 3")
140+
t4 = threading.Thread(
141+
target=a_daemon_thread_running_in_background, name="Daemon Thread", daemon=True
142+
)
143+
t5 = threading.Thread(target=a_non_daemon_thread, name="Non-Daemon Thread")
144+
t4.start()
145+
t1.start()
146+
t2.start()
147+
t1.join()
148+
t2.join()
149+
t3.start()
150+
t5.start()
151+
152+
153+
#
154+
155+
156+
if __name__ == "__main__":
157+
print(f"This is the main thread with id: {threading.get_native_id()}")
158+
create_thread_from_function()
159+
create_thread_from_class()
160+
create_thread_from_function_and_pass_arguments()
161+
create_two_threads_and_join_them()
162+
create_daemon_thread()
163+
combine_all_and_give_names_to_threads()

Week07/estimate_pi.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import random
2+
import threading
3+
from tqdm import tqdm
4+
5+
6+
def throw_points(n):
7+
total_points = 0
8+
inner_points = 0
9+
progress_bar = tqdm(total=n)
10+
for i in range(n):
11+
x = random.random()
12+
y = random.random()
13+
if x**2 + y**2 <= 1:
14+
inner_points += 1
15+
total_points += 1
16+
progress_bar.update(1)
17+
18+
pi = 4 * inner_points / total_points
19+
return pi, inner_points, total_points
20+
21+
22+
if __name__ == "__main__":
23+
total_points = 10000000
24+
number_of_threads = 4
25+
number_of_points_per_thread = total_points // number_of_threads
26+
threads = []
27+
for i in range(number_of_threads):
28+
threads.append(
29+
threading.Thread(
30+
target=throw_points,
31+
args=(number_of_points_per_thread,),
32+
name=f"Throw Points - {i}",
33+
)
34+
)
35+
for thread in threads:
36+
thread.start()
37+
for thread in threads:
38+
thread.join()

0 commit comments

Comments
 (0)