Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng Python
6 để phát triển các chương trình đa luồngCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Giới thiệu về lớp Python ThreadPoolExecutor
Trong hướng dẫn đa luồng, bạn đã học cách quản lý nhiều luồng trong một chương trình bằng cách sử dụng lớp
7 của mô-đunCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
8. LớpCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
7 rất hữu ích khi bạn muốn tạo chủ đề theo cách thủ côngCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Tuy nhiên, quản lý thủ công các luồng không hiệu quả vì việc tạo và hủy nhiều luồng thường xuyên rất tốn kém về chi phí tính toán
Thay vì làm như vậy, bạn có thể muốn sử dụng lại các luồng nếu bạn muốn chạy nhiều tác vụ đặc biệt trong chương trình. Một nhóm chủ đề cho phép bạn đạt được điều này
nhóm chủ đề
Nhóm luồng là một mẫu để đạt được sự thực thi đồng thời trong một chương trình. Nhóm luồng cho phép bạn tự động quản lý nhóm luồng một cách hiệu quả
Mỗi thread trong pool được gọi là worker thread hoặc worker. Nhóm luồng cho phép bạn sử dụng lại các luồng công nhân sau khi các tác vụ được hoàn thành. Nó cũng bảo vệ chống lại những lỗi không mong muốn như ngoại lệ
Thông thường, nhóm luồng cho phép bạn định cấu hình số lượng luồng công nhân và cung cấp quy ước đặt tên cụ thể cho từng luồng công nhân
Để tạo nhóm luồng, bạn sử dụng lớp
6 từ mô-đunCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
1Code language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
ThreadPoolExecutor
Lớp
6 mở rộng lớpCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
3 và trả về một đối tượngCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
4Code language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
người thi hành
Lớp
3 có ba phương thức để điều khiển nhóm luồngCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
6 – gửi một hàm để thực thi và trả về một đối tượng
Code language: Python [python]def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
4. Phương thức
Code language: Python [python]def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
8 nhận một hàm và thực thi nó không đồng bộ
Code language: Python [python]def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
9 – thực thi một chức năng không đồng bộ cho từng phần tử trong một lần lặp
Code language: Python [python]def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
40 – tắt người thi hành
Code language: Python [python]Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Khi bạn tạo một thể hiện mới của lớp
6, Python bắt đầu lớp
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Code language: Python [python]
3Code language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
Sau khi hoàn thành làm việc với người thi hành, bạn phải gọi phương thức
40 một cách rõ ràng để giải phóng tài nguyên do người thi hành nắm giữ. Để tránh gọi phương thứcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
40 một cách rõ ràng, bạn có thể sử dụng trình quản lý ngữ cảnhCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
đối tượng tương lai
Một
4 là một đối tượng đại diện cho kết quả cuối cùng của một hoạt động không đồng bộ. Lớp Future có hai phương thức hữu íchCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
46 – trả về kết quả của hoạt động không đồng bộ
Code language: Python [python]Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
47 – trả về ngoại lệ của hoạt động không đồng bộ trong trường hợp ngoại lệ xảy ra
Code language: Python [python]Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Các ví dụ về ThreadPoolExecutor của Python
Chương trình sau sử dụng một luồng duy nhất
2Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
đầu ra
Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Làm thế nào nó hoạt động
Đầu tiên, xác định hàm
48 mất khoảng một giây để hoàn thành. HàmCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
48 gọi hàmCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
60 để mô phỏng độ trễCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Code language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
Thứ hai, gọi hàm
48 hai lần và in ra kết quả. Trước và sau khi gọi hàmCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
48, chúng ta sử dụngCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
63 để đo thời gian bắt đầu và kết thúcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
4Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Thứ ba, in ra thời gian chương trình chạy
6
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Code language: Python [python]
Bởi vì chức năng
64 mất một giây, gọi nó hai lần sẽ mất khoảng 2 giâyCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Sử dụng ví dụ về phương thức submit[]
Để chạy đồng thời chức năng
64, bạn có thể sử dụng lớpCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
6Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
6Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
đầu ra
7Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Đầu ra cho thấy chương trình mất khoảng 1 giây để hoàn thành
Cách thức hoạt động [chúng tôi sẽ tập trung vào phần nhóm luồng]
Đầu tiên, nhập lớp
6 từ mô-đunCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
1Code language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
0Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Thứ hai, tạo nhóm luồng bằng cách sử dụng
6 bằng trình quản lý bối cảnhCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
2Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Thứ ba, gọi hàm
64 hai lần bằng cách chuyển nó tới phương thứcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
8 của người thi hànhCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
5Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Phương thức
8 trả về một đối tượng Tương lai. Trong ví dụ này, chúng ta có hai đối tượng Future làCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
63 vàCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
64. Để lấy kết quả từ đối tượng Future, chúng ta gọi phương thứcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
46 của nóCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Sử dụng ví dụ về phương thức map[]
Chương trình sau sử dụng một lớp
6. Tuy nhiên, thay vì sử dụng phương thứcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
8, nó sử dụng phương thứcCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
9 để thực thi một hàmCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
0Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Làm thế nào nó hoạt động
Đầu tiên, gọi phương thức
7 của đối tượng thực thi để chạy hàm tác vụ cho từng id trong danh sách [1,2]. Phương thứcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
7 trả về một iterator chứa kết quả của các lệnh gọi hàmCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
1Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Thứ hai, lặp lại các kết quả và in chúng ra
2Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Python ThreadPoolExecutor ví dụ thực tế
Chương trình sau tải xuống nhiều hình ảnh từ Wikipedia bằng nhóm luồng
3Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Làm thế nào nó hoạt động
Đầu tiên, hãy xác định một hàm
71 để tải xuống một hình ảnh từ một URL và lưu nó vào một tệpCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
4Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Chức năng
71 chức năngCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
73 từ mô-đunCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
74 để tải xuống hình ảnh từ một URLCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
Thứ hai, thực thi hàm
71 bằng nhóm luồng bằng cách gọi phương thứcCode language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.
9 của đối tượngCode language: Python [python]
def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'
6Code language: Python [python]
Starting the task 1... Done with task 1 Starting the task 2... Done with task 2 It took 2.0144479 second[s] to finish.