mirror of
https://github.com/yakimka/python_interview_questions.git
synced 2025-12-18 03:54:35 +03:00
Update questions.md (#13)
update Что такое async/await, для чего они нужны и как их использовать
This commit is contained in:
@@ -1437,8 +1437,9 @@ import aiohttp
|
||||
urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']
|
||||
|
||||
async def call_url(url):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
print('Starting {}'.format(url))
|
||||
response = await aiohttp.get(url)
|
||||
async with session.get(url) as response:
|
||||
data = await response.text()
|
||||
print('{}: {} bytes: {}'.format(url, len(data), data))
|
||||
return data
|
||||
@@ -1446,7 +1447,7 @@ async def call_url(url):
|
||||
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