mirror of
https://github.com/yakimka/python_interview_questions.git
synced 2025-12-18 12:04:34 +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']
|
urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']
|
||||||
|
|
||||||
async def call_url(url):
|
async def call_url(url):
|
||||||
print('Starting {}'.format(url))
|
async with aiohttp.ClientSession() as session:
|
||||||
response = await aiohttp.get(url)
|
print('Starting {}'.format(url))
|
||||||
data = await response.text()
|
async with session.get(url) as response:
|
||||||
print('{}: {} bytes: {}'.format(url, len(data), data))
|
data = await response.text()
|
||||||
return data
|
print('{}: {} bytes: {}'.format(url, len(data), data))
|
||||||
|
return data
|
||||||
|
|
||||||
futures = [call_url(url) for url in urls]
|
futures = [call_url(url) for url in urls]
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.run_until_complete(asyncio.wait(futures))
|
loop.run_until_complete(asyncio.gather(*futures))
|
||||||
```
|
```
|
||||||
|
|
||||||
Программа состоит из метода async. Во время выполнения он возвращает сопрограмму, которая затем находится в ожидании.
|
Программа состоит из метода async. Во время выполнения он возвращает сопрограмму, которая затем находится в ожидании.
|
||||||
|
|||||||
Reference in New Issue
Block a user