sequential, threaded & async HTTP clients using HTTPX

This commit is contained in:
Luciano Ramalho
2021-10-02 17:12:42 -03:00
parent 7985fda09f
commit 4f1392d21c
12 changed files with 271 additions and 224 deletions

View File

@@ -21,12 +21,12 @@ import time
from pathlib import Path
from typing import Callable
import requests # <1>
import httpx # <1>
POP20_CC = ('CN IN US ID BR PK NG BD RU JP '
'MX PH VN ET EG DE IR TR CD FR').split() # <2>
BASE_URL = 'http://fluentpython.com/data/flags' # <3>
BASE_URL = 'https://www.fluentpython.com/data/flags' # <3>
DEST_DIR = Path('downloaded') # <4>
def save_flag(img: bytes, filename: str) -> None: # <5>
@@ -34,22 +34,25 @@ def save_flag(img: bytes, filename: str) -> None: # <5>
def get_flag(cc: str) -> bytes: # <6>
url = f'{BASE_URL}/{cc}/{cc}.gif'.lower()
resp = requests.get(url)
resp = httpx.get(url, timeout=6.1, # <7>
follow_redirects=True) # <8>
resp.raise_for_status() # <9>
return resp.content
def download_many(cc_list: list[str]) -> int: # <7>
for cc in sorted(cc_list): # <8>
def download_many(cc_list: list[str]) -> int: # <10>
for cc in sorted(cc_list): # <11>
image = get_flag(cc)
save_flag(image, f'{cc}.gif')
print(cc, end=' ', flush=True) # <9>
print(cc, end=' ', flush=True) # <12>
return len(cc_list)
def main(downloader: Callable[[list[str]], int]) -> None: # <10>
t0 = time.perf_counter() # <11>
def main(downloader: Callable[[list[str]], int]) -> None: # <13>
DEST_DIR.mkdir(exist_ok=True) # <14>
t0 = time.perf_counter() # <15>
count = downloader(POP20_CC)
elapsed = time.perf_counter() - t0
print(f'\n{count} downloads in {elapsed:.2f}s')
if __name__ == '__main__':
main(download_many) # <12>
main(download_many) # <16>
# end::FLAGS_PY[]