-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAsynchronous.py
61 lines (45 loc) · 1.41 KB
/
Asynchronous.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
56
57
58
59
60
61
# import concurrent.futures
# import requests
# import threading
# import time
# thread_local = threading.local()
# def get_session():
# if not hasattr(thread_local, "session"):
# thread_local.session = requests.Session()
# return thread_local.session
# def download_site(url):
# session = get_session()
# with session.get(url) as response:
# print(f"Read {len(response.content)} from {url}")
# def download_all_sites(sites):
# with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
# executor.map(download_site, sites)
# if __name__ == "__main__":
# sites = [
# "https://www.dinamalar.com",
# "https://thehindu.com",
# ] * 80
# start_time = time.time()
# download_all_sites(sites)
# duration = time.time() - start_time
# print(f"Downloaded {len(sites)} in {duration} seconds")
L=len
K=print
import concurrent.futures,requests as D,threading as E,time as B
import google_auth_oauthlib
from tabnanny import check
A=E.local()
def F():
if not hasattr(A,'session'):A.session=D.Session()
return A.session
def G(url):
A=F()
with A.get(url)as B:K(f"Read {L(B.content)} from {url}")
def H(sites):
with concurrent.futures.ThreadPoolExecutor(max_workers=10)as A:A.map(G,sites)
if __name__=='__main__':
C=['https://www.dinamalar.com','https://thehindu.com']*80;
I=B.time()
H(C)
J=B.time()-I
K(f"Downloaded {L(C)} in {J} seconds")