-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgevent.py
72 lines (62 loc) · 1.81 KB
/
gevent.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
from gevent.pool import Pool
from gevent import monkey
# monkey.patch_all()
monkey.patch_socket()
monkey.patch_ssl()
import threading,time,requests,multiprocessing,gevent,aiohttp,asyncio
from lxml import etree
#多线程的join分开写后速度快很多,
#打开10个网站,单线程时间8.8s , 多线程0.8s , gevent4.5s , aiohttp2s
url='https://bbs.hupu.com/topic-{}'
def p(page):
r1=requests.get(url.format(page+1))
r=etree.HTML(r1.text)
a=r.xpath('//a[@class="truetit"]')
for i in range(5):
print(a[i].xpath('string(.)')[:11])
print('='*11)
async def pp(page):
async with aiohttp.ClientSession() as se:
await fetch(se,page)
async def fetch(se,page):
async with se.get(url.format(page+1)) as r1:
r=etree.HTML(await r1.text())
a=r.xpath('//a[@class="truetit"]')
for i in range(5):
print(a[i].xpath('string(.)')[:11])
print('='*11)
if __name__=='__main__':
b=10
t=time.clock()
for i in range(b):
p(i)
print('什么都不用',time.clock()-t)
#8.8
t=time.clock()
ts=[]
for i in range(b):
th=threading.Thread(target=p,args=(i,))
ts.append(th)
th.start()
for th in ts:
th.join()
print('多线程',time.clock()-t)
#0.8
t=time.clock()
ts=[]
for i in range(b):
th=gevent.spawn(p(i))
ts.append(th)
gevent.joinall(ts)
print('gevent',time.clock()-t)
#4.5
t=time.clock()
ts=[]
loop=asyncio.get_event_loop()
for i in range(b):
th=asyncio.ensure_future(pp(i))
ts.append(th)
loop.run_until_complete(asyncio.wait(ts))
loop.close()
print('协程',time.clock()-t)
#2.0