-
Notifications
You must be signed in to change notification settings - Fork 0
/
multithreading_basic.py
39 lines (27 loc) · 956 Bytes
/
multithreading_basic.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
import os
import threading
def print_numbers():
thread_name = threading.current_thread().name
for i in range(1, 6):
print(f"Number: {i} - Thread: {thread_name} - ProcessID: {os.getpid()} ")
def print_letters():
thread_name = threading.current_thread().name
for letter in 'abcde':
print(f"Letter: {letter} - Thread: {thread_name} - ProcessID: {os.getpid()} ")
def thread_function():
# Create threads
number_thread = threading.Thread(target=print_numbers, name="NumberThread")
letter_thread = threading.Thread(target=print_letters, name="LetterThread")
# Start threads
number_thread.start()
letter_thread.start()
# Wait for threads to complete
number_thread.join()
letter_thread.join()
def main():
"""Main thread function starting."""
print("Running main thread function...\n")
thread_function()
print(f"Threads executed.")
if __name__ == "__main__":
main()