首頁  >  文章  >  後端開發  >  了解 Python 中的線程和多重處理:綜合指南

了解 Python 中的線程和多重處理:綜合指南

PHPz
PHPz原創
2024-09-12 14:17:02426瀏覽

Understanding Threading and Multiprocessing in Python: A Comprehensive Guide

介紹

在 Python 中,在最佳化應用程式效能時,特別是當它們涉及並發或並行執行時,經常會討論 執行緒多處理 的概念。儘管術語有重疊,但這兩種方法本質上是不同的。

本部落格將有助於澄清線程多處理的混淆,解釋何時使用每個概念,並為每個概念提供相關範例。


執行緒與多處理:主要差異

在深入範例和用例之前,讓我們先概述一下主要區別:

  • 執行緒:是指在單一行程中執行多個執行緒(進程的較小單元)。線程共享相同的記憶體空間,這使得它們變得輕量級。然而,Python 的全域解釋器鎖定 (GIL) 限制了 CPU 密集型任務的執行緒的真正並行性。

  • 多處理:涉及運行多個進程,每個進程都有自己的記憶體空間。進程比線程重,但可以實現真正的並行性,因為它們不共享記憶體。這種方法非常適合需要充分利用核心的 CPU 密集型任務。


什麼是線程?

執行緒是一種在同一進程中同時執行多個任務的方法。這些任務由執行緒處理,它們是共享相同記憶體空間的獨立的輕量級執行單元。執行緒有利於 I/O 密集型操作,例如檔案讀取、網路請求或資料庫查詢,這些操作中主程式花費大量時間等待外部資源。

何時使用執行緒

  • 當您的程式受 I/O 限制時(例如,讀取/寫入檔案、發出網路請求)。
  • 當任務花費大量時間等待輸入或輸出操作。
  • 當您需要在單一進程中實現輕量級並發時。

範例:基本執行緒

import threading
import time

def print_numbers():
    for i in range(5):
        print(i)
        time.sleep(1)

def print_letters():
    for letter in ['a', 'b', 'c', 'd', 'e']:
        print(letter)
        time.sleep(1)

# Create two threads
t1 = threading.Thread(target=print_numbers)
t2 = threading.Thread(target=print_letters)

# Start both threads
t1.start()
t2.start()

# Wait for both threads to complete
t1.join()
t2.join()

print("Both threads finished execution.")

在上面的範例中,兩個執行緒並發運行:一個列印數字,另一個列印字母。 sleep() 呼叫模擬 I/O 操作,程式可以在這些等待期間在執行緒之間切換。

線程問題:全域解釋器鎖定 (GIL)

Python 的 GIL 是一種防止多個本機執行緒同時執行 Python 位元組碼的機制。它確保一次只有一個執行緒運行,即使進程中有多個執行緒處於活動狀態。

此限制使得執行緒不適合需要真正並行性的 CPU 密集型任務,因為由於 GIL,執行緒無法充分利用多個核心。


什麼是多重處理?

多處理允許您同時運行多個進程,其中每個進程都有自己的記憶體空間。由於進程不共享內存,因此沒有 GIL 限制,允許在多個 CPU 核心上真正並行執行。多重處理非常適合需要最大化 CPU 使用率的 CPU 密集型任務。

何時使用多重處理

  • 當您的程式受 CPU 限制時(例如,執行繁重的計算、資料處理)。
  • 當您需要真正的並行性而不需要記憶體共享時。
  • 當您想要同時執行獨立任務的多個實例時。

範例:基本多重處理

import multiprocessing
import time

def print_numbers():
    for i in range(5):
        print(i)
        time.sleep(1)

def print_letters():
    for letter in ['a', 'b', 'c', 'd', 'e']:
        print(letter)
        time.sleep(1)

if __name__ == "__main__":
    # Create two processes
    p1 = multiprocessing.Process(target=print_numbers)
    p2 = multiprocessing.Process(target=print_letters)

    # Start both processes
    p1.start()
    p2.start()

    # Wait for both processes to complete
    p1.join()
    p2.join()

    print("Both processes finished execution.")

在此範例中,兩個單獨的進程同時運行。與執行緒不同,每個行程都有自己的記憶體空間,並且獨立執行,不受GIL的干擾。

多處理中的記憶體隔離

執行緒和多處理之間的一個關鍵區別是進程不共享記憶體。雖然這確保了進程之間沒有乾擾,但這也意味著它們之間共享資料需要特殊的機制,例如多處理模組提供的 Queue、Pipe 或 Manager 物件。


執行緒與多處理:選取正確的工具

現在我們了解了這兩種方法的工作原理,讓我們根據任務類型詳細說明何時選擇線程或多處理:

Use Case Type Why?
Network requests, I/O-bound tasks (file read/write, DB calls) Threading Multiple threads can handle I/O waits concurrently.
CPU-bound tasks (data processing, calculations) Multiprocessing True parallelism is possible by utilizing multiple cores.
Task requires shared memory or lightweight concurrency Threading Threads share memory and are cheaper in terms of resources.
Independent tasks needing complete isolation (e.g., separate processes) Multiprocessing Processes have isolated memory, making them safer for independent tasks.

Performance Considerations

Threading Performance

Threading excels in scenarios where the program waits on external resources (disk I/O, network). Since threads can work concurrently during these wait times, threading can help boost performance.

However, due to the GIL, CPU-bound tasks do not benefit much from threading because only one thread can execute at a time.

Multiprocessing Performance

Multiprocessing allows true parallelism by running multiple processes across different CPU cores. Each process runs in its own memory space, bypassing the GIL and making it ideal for CPU-bound tasks.

However, creating processes is more resource-intensive than creating threads, and inter-process communication can slow things down if there's a lot of data sharing between processes.


A Practical Example: Threading vs. Multiprocessing for CPU-bound Tasks

Let's compare threading and multiprocessing for a CPU-bound task like calculating the sum of squares for a large list.

Threading Example for CPU-bound Task

import threading

def calculate_squares(numbers):
    result = sum([n * n for n in numbers])
    print(result)

numbers = range(1, 10000000)
t1 = threading.Thread(target=calculate_squares, args=(numbers,))
t2 = threading.Thread(target=calculate_squares, args=(numbers,))

t1.start()
t2.start()

t1.join()
t2.join()

Due to the GIL, this example will not see significant performance improvements over a single-threaded version because the threads can't run simultaneously for CPU-bound operations.

Multiprocessing Example for CPU-bound Task

import multiprocessing

def calculate_squares(numbers):
    result = sum([n * n for n in numbers])
    print(result)

if __name__ == "__main__":
    numbers = range(1, 10000000)
    p1 = multiprocessing.Process(target=calculate_squares, args=(numbers,))
    p2 = multiprocessing.Process(target=calculate_squares, args=(numbers,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

In the multiprocessing example, you'll notice a performance boost since both processes run in parallel across different CPU cores, fully utilizing the machine's computational resources.


Conclusion

Understanding the difference between threading and multiprocessing is crucial for writing efficient Python programs. Here’s a quick recap:

  • Use threading for I/O-bound tasks where your program spends a lot of time waiting for resources.
  • Use multiprocessing for CPU-bound tasks to maximize performance through parallel execution.

Knowing when to use which approach can lead to significant performance improvements and efficient use of resources.

以上是了解 Python 中的線程和多重處理:綜合指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn