|
| 1 | +# Threading in Python |
| 2 | +Threading is a sequence of instructions in a program that can be executed independently of the remaining process and |
| 3 | +Threads are like lightweight processes that share the same memory space but can execute independently. |
| 4 | +The process is an executable instance of a computer program. |
| 5 | +This guide provides an overview of the threading module and its key functionalities. |
| 6 | + |
| 7 | +## Key Characteristics of Threads: |
| 8 | +* Shared Memory: All threads within a process share the same memory space, which allows for efficient communication between threads. |
| 9 | +* Independent Execution: Each thread can run independently and concurrently. |
| 10 | +* Context Switching: The operating system can switch between threads, enabling concurrent execution. |
| 11 | + |
| 12 | +## Threading Module |
| 13 | +This module will allows you to create and manage threads easily. This module includes several functions and classes to work with threads. |
| 14 | + |
| 15 | +**1. Creating Thread:** |
| 16 | +To create a thread in Python, you can use the Thread class from the threading module. |
| 17 | + |
| 18 | +Example: |
| 19 | +```python |
| 20 | +import threading |
| 21 | + |
| 22 | +# Create a thread |
| 23 | +thread = threading.Thread() |
| 24 | + |
| 25 | +# Start the thread |
| 26 | +thread.start() |
| 27 | + |
| 28 | +# Wait for the thread to complete |
| 29 | +thread.join() |
| 30 | + |
| 31 | +print("Thread has finished execution.") |
| 32 | +``` |
| 33 | +Output : |
| 34 | +``` |
| 35 | +Thread has finished execution. |
| 36 | +``` |
| 37 | +**2. Performing Task with Thread:** |
| 38 | +We can also perform a specific task by thread by giving a function as target and its argument as arg ,as a parameter to Thread object. |
| 39 | + |
| 40 | +Example: |
| 41 | + |
| 42 | +```python |
| 43 | +import threading |
| 44 | + |
| 45 | +# Define a function that will be executed by the thread |
| 46 | +def print_numbers(arg): |
| 47 | + for i in range(arg): |
| 48 | + print(f"Thread: {i}") |
| 49 | +# Create a thread |
| 50 | +thread = threading.Thread(target=print_numbers,args=(5,)) |
| 51 | + |
| 52 | +# Start the thread |
| 53 | +thread.start() |
| 54 | + |
| 55 | +# Wait for the thread to complete |
| 56 | +thread.join() |
| 57 | + |
| 58 | +print("Thread has finished execution.") |
| 59 | +``` |
| 60 | +Output : |
| 61 | +``` |
| 62 | +Thread: 0 |
| 63 | +Thread: 1 |
| 64 | +Thread: 2 |
| 65 | +Thread: 3 |
| 66 | +Thread: 4 |
| 67 | +Thread has finished execution. |
| 68 | +``` |
| 69 | +**3. Delaying a Task with Thread's Timer Function:** |
| 70 | +We can set a time for which we want a thread to start. Timer function takes 4 arguments (interval,function,args,kwargs). |
| 71 | + |
| 72 | +Example: |
| 73 | +```python |
| 74 | +import threading |
| 75 | + |
| 76 | +# Define a function that will be executed by the thread |
| 77 | +def print_numbers(arg): |
| 78 | + for i in range(arg): |
| 79 | + print(f"Thread: {i}") |
| 80 | +# Create a thread after 3 seconds |
| 81 | +thread = threading.Timer(3,print_numbers,args=(5,)) |
| 82 | + |
| 83 | +# Start the thread |
| 84 | +thread.start() |
| 85 | + |
| 86 | +# Wait for the thread to complete |
| 87 | +thread.join() |
| 88 | + |
| 89 | +print("Thread has finished execution.") |
| 90 | +``` |
| 91 | +Output : |
| 92 | +``` |
| 93 | +# after three second output will be generated |
| 94 | +Thread: 0 |
| 95 | +Thread: 1 |
| 96 | +Thread: 2 |
| 97 | +Thread: 3 |
| 98 | +Thread: 4 |
| 99 | +Thread has finished execution. |
| 100 | +``` |
| 101 | +**4. Creating Multiple Threads** |
| 102 | +We can create and manage multiple threads to achieve concurrent execution. |
| 103 | + |
| 104 | +Example: |
| 105 | +```python |
| 106 | +import threading |
| 107 | + |
| 108 | +def print_numbers(thread_name): |
| 109 | + for i in range(5): |
| 110 | + print(f"{thread_name}: {i}") |
| 111 | + |
| 112 | +# Create multiple threads |
| 113 | +thread1 = threading.Thread(target=print_numbers, args=("Thread 1",)) |
| 114 | +thread2 = threading.Thread(target=print_numbers, args=("Thread 2",)) |
| 115 | + |
| 116 | +# Start the threads |
| 117 | +thread1.start() |
| 118 | +thread2.start() |
| 119 | + |
| 120 | +# Wait for both threads to complete |
| 121 | +thread1.join() |
| 122 | +thread2.join() |
| 123 | + |
| 124 | +print("Both threads have finished execution.") |
| 125 | +``` |
| 126 | +Output : |
| 127 | +``` |
| 128 | +Thread 1: 0 |
| 129 | +Thread 1: 1 |
| 130 | +Thread 2: 0 |
| 131 | +Thread 1: 2 |
| 132 | +Thread 1: 3 |
| 133 | +Thread 2: 1 |
| 134 | +Thread 2: 2 |
| 135 | +Thread 2: 3 |
| 136 | +Thread 2: 4 |
| 137 | +Thread 1: 4 |
| 138 | +Both threads have finished execution. |
| 139 | +``` |
| 140 | + |
| 141 | +**5. Thread Synchronization** |
| 142 | +When we create multiple threads and they access shared resources, there is a risk of race conditions and data corruption. To prevent this, you can use synchronization primitives such as locks. |
| 143 | +A lock is a synchronization primitive that ensures that only one thread can access a shared resource at a time. |
| 144 | + |
| 145 | +Example: |
| 146 | +```Python |
| 147 | +import threading |
| 148 | + |
| 149 | +lock = threading.Lock() |
| 150 | + |
| 151 | +def print_numbers(thread_name): |
| 152 | + for i in range(10): |
| 153 | + with lock: |
| 154 | + print(f"{thread_name}: {i}") |
| 155 | + |
| 156 | +# Create multiple threads |
| 157 | +thread1 = threading.Thread(target=print_numbers, args=("Thread 1",)) |
| 158 | +thread2 = threading.Thread(target=print_numbers, args=("Thread 2",)) |
| 159 | + |
| 160 | +# Start the threads |
| 161 | +thread1.start() |
| 162 | +thread2.start() |
| 163 | + |
| 164 | +# Wait for both threads to complete |
| 165 | +thread1.join() |
| 166 | +thread2.join() |
| 167 | + |
| 168 | +print("Both threads have finished execution.") |
| 169 | +``` |
| 170 | +Output : |
| 171 | +``` |
| 172 | +Thread 1: 0 |
| 173 | +Thread 1: 1 |
| 174 | +Thread 1: 2 |
| 175 | +Thread 1: 3 |
| 176 | +Thread 1: 4 |
| 177 | +Thread 1: 5 |
| 178 | +Thread 1: 6 |
| 179 | +Thread 1: 7 |
| 180 | +Thread 1: 8 |
| 181 | +Thread 1: 9 |
| 182 | +Thread 2: 0 |
| 183 | +Thread 2: 1 |
| 184 | +Thread 2: 2 |
| 185 | +Thread 2: 3 |
| 186 | +Thread 2: 4 |
| 187 | +Thread 2: 5 |
| 188 | +Thread 2: 6 |
| 189 | +Thread 2: 7 |
| 190 | +Thread 2: 8 |
| 191 | +Thread 2: 9 |
| 192 | +Both threads have finished execution. |
| 193 | +``` |
| 194 | + |
| 195 | +A ```lock``` object is created using threading.Lock() and The ```with lock``` statement ensures that the lock is acquired before printing and released after printing. This prevents other threads from accessing the print statement simultaneously. |
| 196 | + |
| 197 | +## Conclusion |
| 198 | +Threading in Python is a powerful tool for achieving concurrency and improving the performance of I/O-bound tasks. By understanding and implementing threads using the threading module, you can enhance the efficiency of your programs. To prevent race situations and maintain data integrity, keep in mind that thread synchronization must be properly managed. |
0 commit comments