From c8bffb9db02f29288e221b0ebcfad7d587b76323 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Thu, 19 Oct 2023 09:00:15 +0200 Subject: [PATCH] aiohttp --- docs/modern_python.md | 106 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/docs/modern_python.md b/docs/modern_python.md index 150a0d2..0ccc9f1 100644 --- a/docs/modern_python.md +++ b/docs/modern_python.md @@ -1371,6 +1371,112 @@ while not q.empty(): --- +### `aiohttp` + +```python +import asyncio +import aiohttp +import time + + +async def download(name, queue): + async with aiohttp.ClientSession() as session: + while not queue.empty(): + url = await queue.get() + print(f"Task named {name} getting URL: {url}") + async with session.get(url) as response: + t = await response.text() + print(f"Task named {name} downloaded {len(t)} characters") + print(f"Task named {name} finished") + + +async def main(): + queue = asyncio.Queue() + + for url in ( + "http://www.root.cz", + "http://duckduckgo.com", + "http://seznam.com", + "https://www.root.cz/programovaci-jazyky/", + "https://www.root.cz/clanky/soubezne-a-paralelne-bezici-ulohy-naprogramovane-v-pythonu/", + "https://github.com/" + ): + await queue.put(url) + + await asyncio.gather( + asyncio.create_task(download(1, queue)), + asyncio.create_task(download(2, queue))) + + +asyncio.run(main()) +``` + +[Zdrojový kód příkladu](https://github.com/tisnik/most-popular-python-libs/blob/master/modern_python/sources//aiohttp1.py) + +--- + +### `aiohttp` + +```python +import aiohttp +import time + + +async def download(name, queue, results): + async with aiohttp.ClientSession() as session: + while not queue.empty(): + url = await queue.get() + t1 = time.time() + print(f"Task named {name} getting URL: {url}") + async with session.get(url) as response: + t = await response.text() + t2 = time.time() + print(f"Task named {name} downloaded {len(t)} characters in {t2-t1} seconds") + await results.put(t2-t1) + print(f"Task named {name} finished") + + +async def main(): + queue = asyncio.Queue() + results = asyncio.Queue() + + t1 = time.time() + + for url in ( + "http://www.root.cz", + "http://duckduckgo.com", + "http://seznam.com", + "https://www.root.cz/programovaci-jazyky/", + "https://www.root.cz/clanky/soubezne-a-paralelne-bezici-ulohy-naprogramovane-v-pythonu/", + "https://www.root.cz/clanky/pywebio-interaktivni-webove-dialogy-a-formulare-v-cistem-pythonu/", + "https://streamlit.io/", + "https://pglet.io/", + "https://www.root.cz/serialy/graficke-uzivatelske-rozhrani-v-pythonu/", + "https://github.com/" + ): + await queue.put(url) + + await asyncio.gather( + asyncio.create_task(download(1, queue, results)), + asyncio.create_task(download(2, queue, results)), + asyncio.create_task(download(3, queue, results))) + + process_time = 0 + while not results.empty(): + process_time += await results.get() + + print(f"Process time: {process_time} seconds") + + t2 = time.time() + print(f"Total time: {t2-t1} seconds") + +asyncio.run(main()) +``` + +[Zdrojový kód příkladu](https://github.com/tisnik/most-popular-python-libs/blob/master/modern_python/sources//aiohttp2.py) + +--- + ## Skupiny výjimek * Přidáno do Pythonu 3.11