-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncioEx3.py
More file actions
62 lines (51 loc) · 1.63 KB
/
Copy pathAsyncioEx3.py
File metadata and controls
62 lines (51 loc) · 1.63 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# rand.py
import asyncio
import random
import aiohttp
import time
import requests
def syncgethtml(url):
r = requests.get(url)
print("Status:", r.status_code)
print("Content-type:", r.headers['content-type'])
r.close()
async def gethtml(url):
session = aiohttp.ClientSession()
response = await session.get(url)
print(url)
print("Async Status:", response.status)
print("Async Content-type:", response.headers['content-type'])
response.close()
await session.close()
return
'''
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
print("Async Status:", response.status)
print("Async Content-type:", response.headers['content-type'])
'''
async def main():
t1 = time.time()
await asyncio.gather(gethtml("http://google.com"),
gethtml("http://python.org"),
gethtml("http://facebook.com"),
gethtml("http://google.com"),
gethtml("http://python.org"),
gethtml("http://facebook.com"))
t2 = time.time()
print(f'Elapsed time {t2-t1}')
t1 = time.time()
syncgethtml("http://google.com")
syncgethtml("http://python.org")
syncgethtml("http://facebook.com")
syncgethtml("http://google.com")
syncgethtml("http://python.org")
syncgethtml("http://facebook.com")
t2 = time.time()
#time.sleep(5)
print(f'Elapsed time {t2 - t1}')
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
#asyncio.run(main())