-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfancy.py
73 lines (55 loc) · 1.91 KB
/
fancy.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
62
63
64
65
66
67
68
69
70
71
72
73
"""
Python知否:如何编写高效网络IO程序
如何侧写程序瓶颈?如何加速网络IO程序?9012年,别再多线程了,Asyncio一行拯救IO
源代码:https://github.com/wangzhe3224/Python-zhifou
欢迎关注微信视频号:Python知否
欢迎关注公众号:泛程序员 - 一个为非计算机专业程序员充电的地方
"""
import re
import logging
import pathlib
import requests
import httpx
import asyncio
logging.basicConfig()
logging.root.setLevel(logging.INFO)
path = pathlib.Path(__file__).parent.resolve()
def find():
logging.info(f"Start...")
with open(f'{path}/webside.md', 'r', encoding="utf-8") as f:
urls = [line.strip() for line in f.readlines()]
pages = []
for url in urls:
pages.append(requests.get(url).text)
https, http = 0, 0
for page in pages:
https += len(re.findall("https://", page))
http += len(re.findall("http://", page))
logging.info(f"{https = } vs {http = }")
logging.info(f"...")
async def find_fast():
logging.info(f"Start...")
with open(f'{path}/webside.md', 'r', encoding="utf-8") as f:
urls = [line.strip() for line in f.readlines()]
async with httpx.AsyncClient() as client:
tasks = (client.get(url) for url in urls)
resp = await asyncio.gather(*tasks)
pages = [res.text for res in resp]
https, http = 0, 0
for page in pages:
https += len(re.findall("https://", page))
http += len(re.findall("http://", page))
logging.info(f"{https = } vs {http = }")
logging.info(f"...")
def main():
import cProfile
import pstats
with cProfile.Profile() as pf:
# find()
asyncio.run(find_fast())
stats = pstats.Stats(pf)
stats.sort_stats(pstats.SortKey.TIME)
stats.print_stats(10)
stats.dump_stats(f"{path}/profiling.prof")
if __name__ == "__main__":
main()