-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample5.py
More file actions
41 lines (29 loc) · 973 Bytes
/
Copy pathexample5.py
File metadata and controls
41 lines (29 loc) · 973 Bytes
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
import aiohttp
import aiofiles
import asyncio
import os
from timeit import default_timer as timer
async def download_html(session, url):
async with session.get(url, ssl=False) as res:
filename = "output/%s.html" % os.path.basename(url)
async with aiofiles.open(filename, "wb") as f:
while True:
chunk = await res.content.read(1024)
if not chunk:
break
await f.write(chunk)
return await res.release()
async def main(url):
async with aiohttp.ClientSession() as session:
await download_html(session, url)
urls = [
"http://packtpub.com",
"http://python.org",
"http://docs.python.org/3/library/asyncio",
"http://aiohttp.readthedocs.io",
"http://google.com",
]
start = timer()
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*(main(url) for url in urls)))
print("Took %.2f seconds." % (timer() - start))