mirror of
https://github.com/yakimka/python_interview_questions.git
synced 2025-12-16 19:17:04 +03:00
Update questions.md (#13)
update Что такое async/await, для чего они нужны и как их использовать
This commit is contained in:
13
questions.md
13
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. Во время выполнения он возвращает сопрограмму, которая затем находится в ожидании.
|
||||
|
||||
Reference in New Issue
Block a user