並發是現代程式設計中的一個重要思想,它允許多個任務同時運行以提高應用程式的效能。
在 Python 中實作並發的方法有多種,其中最著名的是執行緒和多處理。
在本文中,我們將詳細探討這兩種方法,了解它們的工作原理,並討論何時使用每種方法,以及實際的程式碼範例。
在我們討論線程和多處理之前,了解並發的含義很重要。
並發是指一個程式可以同時執行多個任務或進程。
這可以使程式更好地利用資源並運行得更快,特別是當它需要執行諸如讀取文件或進行大量計算之類的操作時。
實現並發的方式主要有兩種:
Python 提供了兩種主要方式來實現並發:
執行緒可讓您在同一進程內執行多個較小的進程單元(稱為執行緒),共享相同的記憶體空間。
執行緒比進程更輕,它們之間的切換更快。
但是,Python 中的執行緒受全域解釋器鎖定 (GIL) 的約束,這確保一次只有一個執行緒可以執行 Python 程式碼。
Python的線程模組提供了一種簡單靈活的方式來建立和管理線程。
讓我們從一個基本範例開始:
import threading import time def print_numbers(): for i in range(5): print(f"Number: {i}") time.sleep(1) # Creating a thread thread = threading.Thread(target=print_numbers) # Starting the thread thread.start() # Wait for the thread to complete thread.join() print("Thread has finished executing") # Output: # Number: 0 # Number: 1 # Number: 2 # Number: 3 # Number: 4 # Thread has finished executing
在此範例中:
執行緒對於 I/O 密集型任務特別有用,例如檔案操作、網路請求或資料庫查詢,在這些任務中程式大部分時間都在等待外部資源。
這是一個使用執行緒模擬下載檔案的範例:
import threading import time def download_file(file_name): print(f"Starting download of {file_name}...") time.sleep(2) # Simulate download time print(f"Finished downloading {file_name}") files = ["file1.zip", "file2.zip", "file3.zip"] threads = [] # Create and start threads for file in files: thread = threading.Thread(target=download_file, args=(file,)) thread.start() threads.append(thread) # Ensure all threads have finished for thread in threads: thread.join() print("All files have been downloaded.") # Output: # Starting download of file1.zip... # Starting download of file2.zip... # Starting download of file3.zip... # Finished downloading file1.zip # Finished downloading file2.zip # Finished downloading file3.zip # All files have been downloaded.
透過為每個文件下載建立和管理單獨的線程,程式可以同時處理多個任務,從而提高整體效率。
程式碼中關鍵步驟如下:
雖然執行緒可以提高 I/O 密集型任務的效能,但它也有限制:
多處理透過使用單獨的進程而不是執行緒來解決執行緒的限制。
每個進程都有自己的記憶體空間和Python解釋器,允許在多核心系統上實現真正的並行。
這使得多重處理成為需要大量計算的任務的理想選擇。
Python 中的多處理模組可讓您輕鬆建立和管理進程。
Let’s start with a basic example:
import multiprocessing import time def print_numbers(): for i in range(5): print(f"Number: {i}") time.sleep(1) if __name__ == "__main__": # Creating a process process = multiprocessing.Process(target=print_numbers) # Starting the process process.start() # Wait for the process to complete process.join() print("Process has finished executing") # Output: # Number: 0 # Number: 1 # Number: 2 # Number: 3 # Number: 4 # Process has finished executing
This example is similar to the threading example, but with processes.
Notice that the process creation and management are similar to threading, but because processes run in separate memory spaces, they are truly concurrent and can run on different CPU cores.
Multiprocessing is particularly beneficial for tasks that are CPU-bound, such as numerical computations or data processing.
Here’s an example that calculates the square of numbers using multiple processes:
import multiprocessing def compute_square(number): return number * number if __name__ == "__main__": numbers = [1, 2, 3, 4, 5] # Create a pool of processes with multiprocessing.Pool() as pool: # Map function to numbers using multiple processes results = pool.map(compute_square, numbers) print("Squares:", results) # Output: # Squares: [1, 4, 9, 16, 25]
Here are the key steps in the code:
Since each process has its own memory space, sharing data between processes requires inter-process communication (IPC) mechanisms.
The multiprocessing module provides several tools for IPC, such as Queue, Pipe, and Value.
Here’s an example using Queue to share data between processes:
import multiprocessing def worker(queue): # Retrieve and process data from the queue while not queue.empty(): item = queue.get() print(f"Processing {item}") if __name__ == "__main__": queue = multiprocessing.Queue() # Add items to the queue for i in range(10): queue.put(i) # Create a pool of processes to process the queue processes = [] for _ in range(4): process = multiprocessing.Process(target=worker, args=(queue,)) processes.append(process) process.start() # Wait for all processes to complete for process in processes: process.join() print("All processes have finished.") # Output: # Processing 0 # Processing 1 # Processing 2 # Processing 3 # Processing 4 # Processing 5 # Processing 6 # Processing 7 # Processing 8 # Processing 9 # All processes have finished.
In this example:
While multiprocessing provides true parallelism, it comes with its own set of challenges:
Choosing between threading and multiprocessing depends on the type of task you're dealing with:
Use Threading:
Use Multiprocessing:
Concurrency in Python is a powerful way to make your applications run faster.
Threading is great for tasks that involve a lot of waiting, like network operations or reading/writing files, but it's not as effective for tasks that require heavy computations because of something called the Global Interpreter Lock (GIL).
On the other hand, multiprocessing allows for true parallelism, making it perfect for CPU-intensive tasks, although it comes with higher overhead and complexity.
無論您是在處理資料、處理多個網路要求,還是進行複雜的運算,Python 的執行緒和多重處理工具都能為您提供所需的功能,使您的程式盡可能高效、快速。
以上是Python 中的並發性與線程和多處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!