From 0b405215412d2c12ef0cdc7296b694fb9bb267a0 Mon Sep 17 00:00:00 2001 From: Anton Romanov <43515791+warmsnow17@users.noreply.github.com> Date: Wed, 21 Feb 2024 04:48:25 +0300 Subject: [PATCH] Update questions.md (#13) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update Что такое async/await, для чего они нужны и как их использовать --- questions.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/questions.md b/questions.md index e6bd48e..b1755ce 100644 --- a/questions.md +++ b/questions.md @@ -1437,16 +1437,17 @@ import aiohttp urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org'] async def call_url(url): - print('Starting {}'.format(url)) - response = await aiohttp.get(url) - data = await response.text() - print('{}: {} bytes: {}'.format(url, len(data), data)) - return data + async with aiohttp.ClientSession() as session: + print('Starting {}'.format(url)) + async with session.get(url) as response: + data = await response.text() + print('{}: {} bytes: {}'.format(url, len(data), data)) + return data futures = [call_url(url) for url in urls] loop = asyncio.get_event_loop() -loop.run_until_complete(asyncio.wait(futures)) +loop.run_until_complete(asyncio.gather(*futures)) ``` Программа состоит из метода async. Во время выполнения он возвращает сопрограмму, которая затем находится в ожидании.