Threading in Python allows you to run multiple threads (smaller units of a process) concurrently within a single process. This can be useful for tasks that can be parallelized or tasks that involve I/O-bound operations where threads can perform other work while waiting for I/O to complete. Python provides a built-in module called threading for working with threads. Here's an overview of threading in Python and an example:
Creating Threads:
To use threads in Python, you'll typically follow these steps:
- Import the
threadingmodule. - Define a function that represents the task you want the thread to perform.
- Create thread objects, specifying the target function.
- Start the threads.
- Wait for the threads to finish if necessary.
Example:
Here's a simple example of using threads to perform two tasks concurrently:
import threading
import time
# Define two functions to be run in separate threads
def task1():
for i in range(5):
print("Task 1 - Iteration", i)
time.sleep(1)
def task2():
for i in range(5):
print("Task 2 - Iteration", i)
time.sleep(1)
# Create thread objects
thread1 = threading.Thread(target=task1)
thread2 = threading.Thread(target=task2)
# Start the threads
thread1.start()
thread2.start()
# Wait for both threads to finish
thread1.join()
thread2.join()
print("Both threads have finished.")In this example:
-
We import the
threadingmodule. -
We define two functions,
task1()andtask2(), which represent two tasks to be performed concurrently. Each task includes a loop to print messages and atime.sleep()to simulate some work. -
We create two thread objects,
thread1andthread2, specifying the target function for each thread. -
We start both threads using the
start()method. -
We use the
join()method to wait for both threads to finish executing their tasks. This ensures that the "Both threads have finished." message is printed only after both threads have completed their work.
When you run this code, you'll see that the two threads execute concurrently, and their messages may be interleaved. This demonstrates how threading can be used to perform multiple tasks concurrently within a Python program.
It's important to note that Python's Global Interpreter Lock (GIL) can limit the effectiveness of multithreading for CPU-bound tasks. For CPU-bound operations, you may want to consider using the multiprocessing module instead to take full advantage of multiple CPU cores.