Hướng dẫn threadpoolexecutor python - threadpoolexecutor python

Thứ ba, 23/06/2020 | 00:00 GMT+7

Show

Các chuỗi Python là một dạng song song cho phép chương trình của bạn chạy nhiều thủ tục cùng một lúc. Tính song song trong Python cũng có thể đạt được bằng cách sử dụng nhiều quy trình, nhưng các stream đặc biệt phù hợp để tăng tốc các ứng dụng liên quan đến lượng I / O (đầu vào / kết quả ) đáng kể.


Ví dụ về hoạt động liên kết I / O bao gồm thực hiện các yêu cầu web và đọc dữ liệu từ các file . Ngược lại với các hoạt động ràng buộc I / O, các hoạt động ràng buộc CPU (như thực hiện phép toán với thư viện chuẩn Python) sẽ không được hưởng lợi nhiều từ các stream Python.

Python 3 bao gồm tiện ích

  • nano wiki_page_function.py
5 để thực thi mã trong một stream .

Trong hướng dẫn này, ta sẽ sử dụng

  • nano wiki_page_function.py
5 để thực hiện các yêu cầu mạng một cách nhanh chóng. Ta sẽ xác định một hàm rất phù hợp để gọi trong các stream , sử dụng
  • nano wiki_page_function.py
5 để thực thi chức năng đó và xử lý kết quả từ các lần thực thi đó.

Đối với hướng dẫn này, ta sẽ thực hiện các yêu cầu mạng để kiểm tra sự tồn tại của các trang Wikipedia .

Lưu ý: Thực tế là các hoạt động liên kết I / O được hưởng lợi nhiều hơn từ các stream so với các hoạt động liên kết CPU là do một đặc điểm riêng trong Python được gọi là khóa thông dịch toàn cục . Nếu muốn, bạn có thể tìm hiểu thêm về khóa thông dịch global của Python trong tài liệu Python chính thức . Thực tế là các hoạt động liên kết I / O được hưởng lợi nhiều hơn từ các stream so với các hoạt động liên kết CPU là do một đặc điểm riêng trong Python được gọi là khóa thông dịch toàn cục . Nếu muốn, bạn có thể tìm hiểu thêm về khóa thông dịch global của Python trong tài liệu Python chính thức .

Yêu cầu

Để tận dụng tối đa hướng dẫn này, bạn nên làm quen với lập trình bằng Python và môi trường lập trình Python local với

  • nano wiki_page_function.py
8 được cài đặt.

Bạn có thể xem lại các hướng dẫn này để biết thông tin cơ bản cần thiết:

  • Cách viết mã bằng Python 3
  • Cách cài đặt Python 3 và cài đặt môi trường lập trình local trên Ubuntu 18.04

  • Để cài đặt gói

    • nano wiki_page_function.py
    8 vào môi trường lập trình Python local của bạn, bạn có thể chạy lệnh sau:

  • pip install --user requests==2.23.0

Bước 1 - Xác định một hàm để thực thi trong chuỗi

Hãy bắt đầu bằng cách xác định một hàm mà ta muốn thực thi với sự trợ giúp của các stream .

Sử dụng

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
0 hoặc môi trường phát triển / soạn thảo văn bản bạn muốn , bạn có thể mở file này:

  • nano wiki_page_function.py

Đối với hướng dẫn này, ta sẽ viết một hàm xác định xem trang Wikipedia có tồn tại hay không:

wiki_page_ Chức năng.py

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 

Các

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 chức năng chấp nhận hai đối số: một URL đến một trang Wikipedia (
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
2 ), và một
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
3 số giây để chờ đợi một phản ứng từ URL đó.

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 sử dụng gói
  • nano wiki_page_function.py
8 để thực hiện yêu cầu web tới URL đó. Tùy thuộc vào mã trạng thái của
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
6 HTTP, một chuỗi được trả về mô tả trang có tồn tại hay không. Các mã trạng thái khác nhau thể hiện các kết quả khác nhau của một yêu cầu HTTP. Quy trình này giả định mã trạng thái
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
7 "thành công" nghĩa là trang Wikipedia tồn tại và mã trạng thái
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
8 "không tìm thấy" nghĩa là trang Wikipedia không tồn tại.

Như được mô tả trong phần Yêu cầu , bạn cần cài đặt gói

  • nano wiki_page_function.py
8 để chạy chức năng này.

Hãy thử chạy hàm bằng cách thêm

. . . url = "https://en.wikipedia.org/wiki/Ocean" print(get_wiki_page_existence(wiki_page_url=url)) 
0 và lệnh gọi hàm sau hàm
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 :

wiki_page_ Chức năng.py

. . . url = "https://en.wikipedia.org/wiki/Ocean" print(get_wiki_page_existence(wiki_page_url=url)) 

Các

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 chức năng chấp nhận hai đối số: một URL đến một trang Wikipedia (
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
2 ), và một
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
3 số giây để chờ đợi một phản ứng từ URL đó.

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 sử dụng gói
  • nano wiki_page_function.py
8 để thực hiện yêu cầu web tới URL đó. Tùy thuộc vào mã trạng thái của
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
6 HTTP, một chuỗi được trả về mô tả trang có tồn tại hay không. Các mã trạng thái khác nhau thể hiện các kết quả khác nhau của một yêu cầu HTTP. Quy trình này giả định mã trạng thái
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
7 "thành công" nghĩa là trang Wikipedia tồn tại và mã trạng thái
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
8 "không tìm thấy" nghĩa là trang Wikipedia không tồn tại.

  • python wiki_page_function.py

Như được mô tả trong phần Yêu cầu , bạn cần cài đặt gói

  • nano wiki_page_function.py
8 để chạy chức năng này.

Output

https://en.wikipedia.org/wiki/Ocean - exists

Hãy thử chạy hàm bằng cách thêm

. . . url = "https://en.wikipedia.org/wiki/Ocean" print(get_wiki_page_existence(wiki_page_url=url)) 
0 và lệnh gọi hàm sau hàm
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 :

Khi bạn đã thêm mã, hãy lưu file . Nói chung, không an toàn khi chia sẻ các đối tượng hoặc trạng thái Python giữa các stream mà không cần chú ý đặc biệt để tránh các lỗi đồng thời. Khi xác định một hàm để thực thi trong một stream , cách tốt nhất là xác định một hàm thực hiện một công việc duy nhất và không chia sẻ hoặc xuất bản trạng thái cho các stream khác.

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 là một ví dụ về một hàm như vậy.

Nếu ta chạy mã này:

Ta sẽ thấy kết quả như sau:

Việc gọi hàm

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 với một trang Wikipedia hợp lệ sẽ trả về một chuỗi xác nhận trên thực tế, trang đó tồn tại.

wiki_page_ Chức năng.py

import requests import concurrent.futures  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status  wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url))     for future in concurrent.futures.as_completed(futures):         print(future.result()) 

Các

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 chức năng chấp nhận hai đối số: một URL đến một trang Wikipedia (
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
2 ), và một
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
3 số giây để chờ đợi một phản ứng từ URL đó.

  • import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
    1 sử dụng gói
    • nano wiki_page_function.py
    8 để thực hiện yêu cầu web tới URL đó. Tùy thuộc vào mã trạng thái của
    import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
    6 HTTP, một chuỗi được trả về mô tả trang có tồn tại hay không. Các mã trạng thái khác nhau thể hiện các kết quả khác nhau của một yêu cầu HTTP. Quy trình này giả định mã trạng thái
    import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
    7 "thành công" nghĩa là trang Wikipedia tồn tại và mã trạng thái
    import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
    8 "không tìm thấy" nghĩa là trang Wikipedia không tồn tại.
  • Như được mô tả trong phần Yêu cầu , bạn cần cài đặt gói
    • nano wiki_page_function.py
    8 để chạy chức năng này.
  • Hãy thử chạy hàm bằng cách thêm
    . . . url = "https://en.wikipedia.org/wiki/Ocean" print(get_wiki_page_existence(wiki_page_url=url)) 
    0 và lệnh gọi hàm sau hàm
    import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
    1 :
  • Khi bạn đã thêm mã, hãy lưu file .
  • Nếu ta chạy mã này:

Ta sẽ thấy kết quả như sau:

  • python wiki_page_function.py

Như được mô tả trong phần Yêu cầu , bạn cần cài đặt gói

  • nano wiki_page_function.py
8 để chạy chức năng này.

Output

https://en.wikipedia.org/wiki/Island - exists https://en.wikipedia.org/wiki/Ocean - exists https://en.wikipedia.org/wiki/this_page_does_not_exist - does not exist https://en.wikipedia.org/wiki/Shark - exists

Hãy thử chạy hàm bằng cách thêm

. . . url = "https://en.wikipedia.org/wiki/Ocean" print(get_wiki_page_existence(wiki_page_url=url)) 
0 và lệnh gọi hàm sau hàm
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 :

Bước 3 - Xử lý ngoại lệ từ các hàm chạy trong chuỗi

Trong bước trước,

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 đã trả về thành công một giá trị cho tất cả các lệnh gọi của ta . Trong bước này, ta sẽ thấy rằng
  • nano wiki_page_function.py
5 cũng có thể nêu ra các ngoại lệ được tạo trong các lệnh gọi hàm stream .

Hãy xem xét khối mã ví dụ sau:

wiki_page_ Chức năng.py

import requests import concurrent.futures   def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status   wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(             executor.submit(                 get_wiki_page_existence, wiki_page_url=url, timeout=0.00001             )         )     for future in concurrent.futures.as_completed(futures):         try:             print(future.result())         except requests.ConnectTimeout:             print("ConnectTimeout.") 

Khối mã này gần giống với khối mà ta đã sử dụng ở Bước 2, nhưng nó có hai điểm khác biệt chính:

  • Bây giờ ta vượt qua

    Output

    https://en.wikipedia.org/wiki/Ocean - exists
    5 để
    import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
    1 . Vì gói
    • nano wiki_page_function.py
    8 sẽ không thể hoàn thành yêu cầu web của nó tới Wikipedia trong

    Output

    https://en.wikipedia.org/wiki/Ocean - exists
    8 giây, nên nó sẽ đưa ra một ngoại lệ

    Output

    https://en.wikipedia.org/wiki/Ocean - exists
    9 .
  • Ta bắt các ngoại lệ

    Output

    https://en.wikipedia.org/wiki/Ocean - exists
    9 được nêu ra bởi
    import requests import concurrent.futures  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status  wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url))     for future in concurrent.futures.as_completed(futures):         print(future.result()) 
    1 và in ra một chuỗi mỗi lần ta làm như vậy.

Nếu ta chạy lại chương trình, ta sẽ thấy kết quả sau:

  • nano wiki_page_function.py
0

Bốn thông báo

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 được in — một thông báo cho mỗi
  • python wiki_page_function.py
3 của ta , vì không ai trong số chúng có thể hoàn thành trong

Output

https://en.wikipedia.org/wiki/Ocean - exists
8 giây và mỗi lệnh trong bốn
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 gọi
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 đã nêu ra ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 .

Đến đây bạn đã thấy rằng nếu một lệnh gọi hàm được gửi đến

  • nano wiki_page_function.py
5 tạo ra một ngoại lệ, thì ngoại lệ đó có thể được nâng lên bình thường bằng cách gọi
import requests import concurrent.futures  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status  wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url))     for future in concurrent.futures.as_completed(futures):         print(future.result()) 
9 . Gọi
import requests import concurrent.futures  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status  wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url))     for future in concurrent.futures.as_completed(futures):         print(future.result()) 
9 trên tất cả các lệnh gọi đã gửi của bạn đảm bảo chương trình của bạn sẽ không bỏ lỡ bất kỳ trường hợp ngoại lệ nào được nêu ra từ hàm stream của bạn.

Bước 4 - So sánh thời gian thực thi có và không có stream

Bây giờ hãy xác minh việc sử dụng

  • nano wiki_page_function.py
5 thực sự làm cho chương trình của bạn nhanh hơn.

Đầu tiên, hãy dành thời gian

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 nếu ta chạy nó mà không có chuỗi:

wiki_page_ Chức năng.py

  • nano wiki_page_function.py
1

Khối mã này gần giống với khối mà ta đã sử dụng ở Bước 2, nhưng nó có hai điểm khác biệt chính:

Bây giờ ta vượt qua

Output

https://en.wikipedia.org/wiki/Ocean - exists
5 để
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 . Vì gói
  • nano wiki_page_function.py
8 sẽ không thể hoàn thành yêu cầu web của nó tới Wikipedia trong

Output

https://en.wikipedia.org/wiki/Ocean - exists
8 giây, nên nó sẽ đưa ra một ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 .

  • nano wiki_page_function.py
2

Ta bắt các ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 được nêu ra bởi
import requests import concurrent.futures  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status  wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url))     for future in concurrent.futures.as_completed(futures):         print(future.result()) 
1 và in ra một chuỗi mỗi lần ta làm như vậy.

Nếu ta chạy lại chương trình, ta sẽ thấy kết quả sau:

Bốn thông báo

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 được in — một thông báo cho mỗi
  • python wiki_page_function.py
3 của ta , vì không ai trong số chúng có thể hoàn thành trong

Output

https://en.wikipedia.org/wiki/Ocean - exists
8 giây và mỗi lệnh trong bốn
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 gọi
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 đã nêu ra ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 .

wiki_page_ Chức năng.py

  • nano wiki_page_function.py
3

Khối mã này gần giống với khối mà ta đã sử dụng ở Bước 2, nhưng nó có hai điểm khác biệt chính:

Bây giờ ta vượt qua

Output

https://en.wikipedia.org/wiki/Ocean - exists
5 để
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 . Vì gói
  • nano wiki_page_function.py
8 sẽ không thể hoàn thành yêu cầu web của nó tới Wikipedia trong

Output

https://en.wikipedia.org/wiki/Ocean - exists
8 giây, nên nó sẽ đưa ra một ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 .

  • nano wiki_page_function.py
4

Ta bắt các ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 được nêu ra bởi
import requests import concurrent.futures  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status  wiki_page_urls = [     "https://en.wikipedia.org/wiki/Ocean",     "https://en.wikipedia.org/wiki/Island",     "https://en.wikipedia.org/wiki/this_page_does_not_exist",     "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor:     futures = []     for url in wiki_page_urls:         futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url))     for future in concurrent.futures.as_completed(futures):         print(future.result()) 
1 và in ra một chuỗi mỗi lần ta làm như vậy.

Nếu ta chạy lại chương trình, ta sẽ thấy kết quả sau:

Bốn thông báo

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 được in — một thông báo cho mỗi
  • python wiki_page_function.py
3 của ta , vì không ai trong số chúng có thể hoàn thành trong

Output

https://en.wikipedia.org/wiki/Ocean - exists
8 giây và mỗi lệnh trong bốn
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 gọi
import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 đã nêu ra ngoại lệ

Output

https://en.wikipedia.org/wiki/Ocean - exists
9 .

Đến đây bạn đã thấy rằng nếu một lệnh gọi hàm được gửi đến nano wiki_page_function.py 5 tạo ra một ngoại lệ, thì ngoại lệ đó có thể được nâng lên bình thường bằng cách gọi import requests import concurrent.futures def get_wiki_page_existence(wiki_page_url, timeout=10): response = requests.get(url=wiki_page_url, timeout=timeout) page_status = "unknown" if response.status_code == 200: page_status = "exists" elif response.status_code == 404: page_status = "does not exist" return wiki_page_url + " - " + page_status wiki_page_urls = [ "https://en.wikipedia.org/wiki/Ocean", "https://en.wikipedia.org/wiki/Island", "https://en.wikipedia.org/wiki/this_page_does_not_exist", "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor: futures = [] for url in wiki_page_urls: futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url)) for future in concurrent.futures.as_completed(futures): print(future.result()) 9 . Gọi import requests import concurrent.futures def get_wiki_page_existence(wiki_page_url, timeout=10): response = requests.get(url=wiki_page_url, timeout=timeout) page_status = "unknown" if response.status_code == 200: page_status = "exists" elif response.status_code == 404: page_status = "does not exist" return wiki_page_url + " - " + page_status wiki_page_urls = [ "https://en.wikipedia.org/wiki/Ocean", "https://en.wikipedia.org/wiki/Island", "https://en.wikipedia.org/wiki/this_page_does_not_exist", "https://en.wikipedia.org/wiki/Shark", ] with concurrent.futures.ThreadPoolExecutor() as executor: futures = [] for url in wiki_page_urls: futures.append(executor.submit(get_wiki_page_existence, wiki_page_url=url)) for future in concurrent.futures.as_completed(futures): print(future.result()) 9 trên tất cả các lệnh gọi đã gửi của bạn đảm bảo chương trình của bạn sẽ không bỏ lỡ bất kỳ trường hợp ngoại lệ nào được nêu ra từ hàm stream của bạn.

Bước 4 - So sánh thời gian thực thi có và không có stream

Bây giờ hãy xác minh việc sử dụng

  • nano wiki_page_function.py
5 thực sự làm cho chương trình của bạn nhanh hơn.


Đầu tiên, hãy dành thời gian

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 nếu ta chạy nó mà không có chuỗi:


Trong ví dụ mã, ta gọi hàm

import requests  def get_wiki_page_existence(wiki_page_url, timeout=10):     response = requests.get(url=wiki_page_url, timeout=timeout)      page_status = "unknown"     if response.status_code == 200:         page_status = "exists"     elif response.status_code == 404:         page_status = "does not exist"      return wiki_page_url + " - " + page_status 
1 với năm mươi URL trang Wikipedia khác nhau từng cái một. Ta sử dụng hàm
  • python wiki_page_function.py
4 để in ra số giây cần thiết để chạy chương trình của ta .

Nếu ta chạy lại mã này như trước, ta sẽ thấy kết quả như sau:
2020-06-02
Cách thiết lập notebook Jupyter với Python 3 trên Ubuntu 20.04 và Kết nối qua Đường hầm SSH
2020-05-19
Cách cài đặt Phân phối Python Anaconda trên Ubuntu 20.04 [Khởi động nhanh]
2020-05-19
Cách cài đặt Phân phối Python Anaconda trên Ubuntu 20.04
2020-05-06
Cách cài đặt Python 3 và thiết lập môi trường lập trình trên server Ubuntu 20.04
2020-04-24
Cách cài đặt Python 3 và thiết lập môi trường lập trình trên server Ubuntu 18.04
2020-04-24
Cách cài đặt Python 3 và thiết lập môi trường lập trình trên Ubuntu 20.04 [Quickstart]
2020-04-24
Cách cài đặt Python 3 và thiết lập môi trường lập trình trên Ubuntu 18.04 [Quickstart]
2020-04-24
Cách cài đặt Python 3 và thiết lập môi trường lập trình trên CentOS 8
2020-04-10
Cách bắt đầu với Python trong Visual Studio Code
2020-04-09