Skip to content

Commit 0b40521

Browse files
authored
Update questions.md (#13)
update Что такое async/await, для чего они нужны и как их использовать
1 parent 345d531 commit 0b40521

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

questions.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,16 +1437,17 @@ import aiohttp
14371437
urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']
14381438

14391439
async def call_url(url):
1440-
print('Starting {}'.format(url))
1441-
response = await aiohttp.get(url)
1442-
data = await response.text()
1443-
print('{}: {} bytes: {}'.format(url, len(data), data))
1444-
return data
1440+
async with aiohttp.ClientSession() as session:
1441+
print('Starting {}'.format(url))
1442+
async with session.get(url) as response:
1443+
data = await response.text()
1444+
print('{}: {} bytes: {}'.format(url, len(data), data))
1445+
return data
14451446

14461447
futures = [call_url(url) for url in urls]
14471448

14481449
loop = asyncio.get_event_loop()
1449-
loop.run_until_complete(asyncio.wait(futures))
1450+
loop.run_until_complete(asyncio.gather(*futures))
14501451
```
14511452

14521453
Программа состоит из метода async. Во время выполнения он возвращает сопрограмму, которая затем находится в ожидании.

0 commit comments

Comments
 (0)