Việc tạo một chuỗi Python có đắt không?

Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng 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]
6 để phát triển các chương trình đa luồng

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

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]
7 của mô-đun

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]
8. 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]
7 rất hữu ích khi bạn muốn tạo chủ đề theo cách thủ công

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

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]
6 từ mô-đun

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
1

ThreadPoolExecutor

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]
6 mở rộng lớp

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
3 và trả về một đối tượng

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
4

người thi hành

Lớp

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
3 có ba phương thức để điều khiển nhóm luồng

  • def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

    Code language: Python [python]
    6 – gửi một hàm để thực thi và trả về một đối tượng

    def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

    Code language: Python [python]
    4. Phương thức

    def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

    Code language: Python [python]
    8 nhận một hàm và thực thi nó không đồng bộ
  • def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

    Code language: Python [python]
    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
  • 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]
    40 – tắt người thi hành

Khi bạn tạo một thể hiện mới của 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]
6, Python bắt đầu lớp

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
3

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

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]
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ức

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]
40 một cách rõ ràng, bạn có thể sử dụng trình quản lý ngữ cảnh

đối tượng tương lai

Một

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
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 ích

  • 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]
    46 – trả về kết quả của hoạt động không đồng bộ
  • 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]
    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

Các ví dụ về ThreadPoolExecutor của Python

Chương trình sau sử dụng một luồng duy nhất

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]
2

đầu ra

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]

Làm thế nào nó hoạt động

Đầu tiên, xác định hàm

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]
48 mất khoảng một giây để hoàn thành. Hàm

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]
48 gọi hàm

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]
60 để mô phỏng độ trễ

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]

Thứ hai, gọi hàm

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]
48 hai lần và in ra kết quả. Trước và sau khi gọi hàm

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]
48, chúng ta sử dụng

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]
63 để đo thời gian bắt đầu và kết thúc

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]
4

Thứ ba, in ra thời gian chương trình chạy

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]
6

Bởi vì chức năng

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]
64 mất một giây, gọi nó hai lần sẽ mất khoảng 2 giây

Sử dụng ví dụ về phương thức submit[]

Để chạy đồng thời chức năng

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]
64, bạn có thể sử dụng 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]
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]
6

đầu ra

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]
7

Đầ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

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]
6 từ mô-đun

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
1

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]
0

Thứ hai, tạo nhóm luồng bằng cách sử dụng

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]
6 bằng trình quản lý bối cảnh

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]
2

Thứ ba, gọi hàm

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]
64 hai lần bằng cách chuyển nó tới phương thức

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
8 của người thi hành

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]
5

Phương thức

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
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à

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]
63 và

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]
64. Để lấy kết quả từ đối tượng Future, chúng ta gọi phương thức

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]
46 của nó

Sử dụng ví dụ về phương thức map[]

Chương trình sau sử dụng một 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]
6. Tuy nhiên, thay vì sử dụng phương thức

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
8, nó sử dụng phương thức

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
9 để thực thi một hàm

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]
0

Làm thế nào nó hoạt động

Đầu tiên, gọi phương thức

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]
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ức

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]
7 trả về một iterator chứa kết quả của các lệnh gọi hàm

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]
1

Thứ hai, lặp lại các kết quả và in chúng ra

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]
2

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

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]
3

Làm thế nào nó hoạt động

Đầu tiên, hãy xác định một hàm

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]
71 để tải xuống một hình ảnh từ một URL và lưu nó vào một tệ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]
4

Chức năng

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]
71 chức năng

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]
73 từ mô-đun

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]
74 để tải xuống hình ảnh từ một URL

Thứ hai, thực thi hàm

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]
71 bằng nhóm luồng bằng cách gọi phương thức

def task[id]: print[f'Starting the task {id}...'] sleep[1] return f'Done with task {id}'

Code language: Python [python]
9 của đối tượng

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]
6

Có tốn kém để tạo chủ đề?

1 câu trả lời. Việc tạo luồng Java rất tốn kém vì có khá nhiều công việc liên quan . Một khối bộ nhớ lớn phải được phân bổ và khởi tạo cho ngăn xếp luồng. Các cuộc gọi hệ thống cần được thực hiện để tạo/đăng ký luồng gốc với Hệ điều hành máy chủ.

Chủ đề sinh sản có đắt không?

Việc tạo luồng rất tốn kém so với việc tạo hầu hết các đối tượng , nhưng không quá đắt so với tìm kiếm ổ cứng ngẫu nhiên.

Chủ đề có rẻ hơn quy trình không?

Chuyển đổi luồng rất hiệu quả và rẻ hơn nhiều vì nó chỉ liên quan đến việc chuyển đổi danh tính và tài nguyên như bộ đếm chương trình, thanh ghi và con trỏ ngăn xếp.

Chủ đề nào đắt hơn?

Luồng nhân bảo trì thường tốn kém hơn so với luồng người dùng vì chúng phải được biểu diễn bằng cấu trúc dữ liệu nhân. 4. 3 Mô tả các hành động được thực hiện bởi kernel để chuyển ngữ cảnh giữa các luồng cấp kernel.

Chủ Đề