-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_gevent.py
58 lines (39 loc) · 960 Bytes
/
test_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
import gevent
from gevent import monkey
import sys
from gevent.pool import Pool
import requests
from redis_inc import RedisQueueConnection
monkey.patch_all(thread=False)
size = 100
pool = Pool(size)
runque = RedisQueueConnection('running').conn
robotsque = RedisQueueConnection('robots').conn
def httpget(url):
url = url + "/robots.txt"
con = ""
try:
with gevent.Timeout(2) as timeout:
req = requests.get(url,timeout=(2,2))
con = req.content
#print url, len(con)
req.close()
except:
pass
data = (url, con)
cb(data)
from time import time
def cb(data):
seed, con = data
#print "\t", seed, len(con)
cnt = 0
sst = time()
while True:
url = runque.get()
runque.put(url)
st = time()
pool.spawn(httpget, url)
et = time()
cnt += 1
if cnt % 10 == 0:
print cnt / (et-sst) ,runque.qsize(), robotsque.qsize()