Python ThreadPoolExecutor ghi vào tệp
Xem trên Github Show single_thread. py
nhiều_threads. py
multiple_threads_using_threading. py Luồng cho phép song song mã và ngôn ngữ Python có hai cách để đạt được thứ nhất là thông qua mô-đun đa xử lý và cách thứ hai là thông qua mô-đun đa luồng. Đa luồng rất phù hợp để tăng tốc các tác vụ liên quan đến I/O như thực hiện yêu cầu web hoặc thao tác cơ sở dữ liệu hoặc đọc/ghi vào tệp. Ngược lại với CPU này, các tác vụ chuyên sâu như các tác vụ tính toán toán học được hưởng lợi nhiều nhất khi sử dụng đa xử lý. Điều này xảy ra do GIL (Khóa phiên dịch toàn cầu) Từ Python 3. 2 trở đi, một lớp mới có tên ThreadPoolExecutor đã được giới thiệu đồng thời trong Python. mô-đun tương lai để quản lý và tạo chủ đề hiệu quả. Nhưng đợi đã nếu python đã có sẵn một mô-đun luồng thì tại sao một mô-đun mới lại được giới thiệu. Hãy để tôi trả lời điều này đầu tiên
Phương thức ThreadPoolExecutorLớp ThreadPoolExecutor hiển thị ba phương thức để thực thi các luồng không đồng bộ. Một lời giải thích chi tiết được đưa ra dưới đây
ví dụ 1 Đoạn mã dưới đây minh họa việc sử dụng ThreadPoolExecutor, lưu ý rằng không giống như mô-đun luồng, chúng tôi không phải gọi rõ ràng bằng cách sử dụng vòng lặp, theo dõi luồng bằng cách sử dụng danh sách hoặc đợi các luồng bằng cách sử dụng nối để đồng bộ hóa hoặc giải phóng tài nguyên sau các luồng Python3 0 1 2 3 0 5 2 7
Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds0 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds1 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds2 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds3 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds4 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds5 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds4 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds7 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds4 Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds9 00
01 02 03 04 05 06 07
08 09____31Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds1 12 13 03 15Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds1 17 03 19____31Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds7 22 23 24____225 07 23 23 29 23 15Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Single Threaded Code Took :2.5529379630024778 seconds ************************************************** Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. Downloading.. MultiThreaded Code Took:0.5221083430078579 seconds1 33 34 35 03 03 38 39____200 01 02 04 04đầu ra 9ví dụ 2 Đoạn mã dưới đây đang tìm nạp hình ảnh qua internet bằng cách thực hiện một yêu cầu HTTP, tôi đang sử dụng thư viện yêu cầu cho cùng một yêu cầu. Phần đầu tiên của mã thực hiện lệnh gọi một đối một tới API và tôi. e quá trình tải xuống chậm, trong khi phần thứ hai của mã đưa ra yêu cầu song song bằng cách sử dụng các luồng để tìm nạp API Bạn có thể thử tất cả các tham số khác nhau được thảo luận ở trên để xem cách nó điều chỉnh tốc độ tăng tốc, ví dụ: nếu tôi tạo nhóm luồng gồm 6 thay vì 3 thì việc tăng tốc sẽ quan trọng hơn Đang ghi vào chủ đề tập tinViệc ghi vào tệp không an toàn cho luồng
. Việc ghi vào cùng một tệp từ nhiều luồng đồng thời không phải là luồng an toàn và có thể dẫn đến tình trạng dồn đuổi.
ThreadPoolExecutor hoạt động như thế nào trong Python?ThreadPoolExecutor. ThreadPoolExecutor là một lớp con Executor sử dụng nhóm luồng để thực hiện lệnh gọi không đồng bộ . Một lớp con Executor sử dụng nhóm tối đa max_workers luồng để thực hiện các cuộc gọi không đồng bộ. Tất cả các chủ đề được liệt kê vào ThreadPoolExecutor sẽ được nối trước khi trình thông dịch có thể thoát.
Đang nối thêm vào một chuỗi tệpViệc thêm một tệp từ nhiều luồng là không an toàn cho luồng và sẽ dẫn đến dữ liệu bị ghi đè và hỏng tệp. Trong hướng dẫn này, bạn sẽ khám phá cách nối thêm vào một tệp từ nhiều luồng.
Là ThreadPoolExecutor chủ đềAn toàn luồng ThreadPoolExecutor
. Tuy nhiên, khi truy cập tài nguyên hoặc các phần quan trọng, sự an toàn của chuỗi có thể là mối quan tâm . |