-
Notifications
You must be signed in to change notification settings - Fork 1
/
42_multiple_call.py
55 lines (44 loc) · 1.42 KB
/
42_multiple_call.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
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
import requests
import datetime
import aiohttp
import asyncio
# print(datetime.datetime.now())
# def normal_approach():
# list_of_url = ["https://www.google.com/", "https://www.bing.com/",
# "https://www.youtube.com/",
# "https://www.amazon.com/", "https://www.flipkart.com/"]
# list_of_url = list_of_url * 5
# print(f"Number of URL: {len(list_of_url)}")
#
# for url in list_of_url:
# r = requests.get(url)
# print(r.status_code)
#
#
# normal_approach()
# print(datetime.datetime.now())
async def fetch(session, url):
async with session.get(url) as response:
return str(response.status)
async def main():
list_of_url = ["https://www.google.com/", "https://www.bing.com/",
"https://www.youtube.com/",
"https://www.amazon.com/", "https://www.flipkart.com/"]
list_of_url = list_of_url * 5
print(f"Number of URL: {len(list_of_url)}")
tasks = []
async with aiohttp.ClientSession() as session:
for url in list_of_url:
# print(url)
tasks.append(fetch(session, url))
htmls = await asyncio.gather(*tasks)
# print(htmls)
# for html in htmls:
# print(html[:100])
def async_approach():
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print(datetime.datetime.now())
async_approach()
print(datetime.datetime.now())