|
| 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() |
0 commit comments