File tree Expand file tree Collapse file tree 1 file changed +7
-6
lines changed Expand file tree Collapse file tree 1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change @@ -1437,16 +1437,17 @@ import aiohttp
1437
1437
urls = [' http://www.google.com' , ' http://www.yandex.ru' , ' http://www.python.org' ]
1438
1438
1439
1439
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
1445
1446
1446
1447
futures = [call_url(url) for url in urls]
1447
1448
1448
1449
loop = asyncio.get_event_loop()
1449
- loop.run_until_complete(asyncio.wait( futures))
1450
+ loop.run_until_complete(asyncio.gather( * futures))
1450
1451
```
1451
1452
1452
1453
Программа состоит из метода async. Во время выполнения он возвращает сопрограмму, которая затем находится в ожидании.
You can’t perform that action at this time.
0 commit comments