-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaiohttp_demo.py
42 lines (27 loc) · 856 Bytes
/
aiohttp_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
"""Demo of aiohttp package."""
import aiohttp
import asyncio
async def fetch(session, url):
"""Fetch the url."""
async with session.get(url) as response:
return await response.text(encoding='utf-8')
async def task(url):
"""Async task function."""
print(f'{url} started.')
async with aiohttp.ClientSession() as session:
html = await fetch(session, url)
print(f'{url} done.')
async def main():
"""Main function."""
tasks = []
urls = ('https://news.ycombinator.com/rss',
'https://planetpython.org/rss20.xml',
'https://python.org',
)
for url in urls:
tasks.append(loop.create_task(task(url)))
await asyncio.wait(tasks)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())