-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreqs.py
32 lines (25 loc) · 921 Bytes
/
reqs.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
import gevent
from gevent import monkey
monkey.patch_all()
import requests, requests.packages
requests.packages.urllib3.disable_warnings()
import urllib2
def worker(url, use_urllib2=False):
if use_urllib2:
return urllib2.urlopen(url).read().lower()
else:
return requests.get(url).text.lower()
urls = ['http://www.github.com']*5
def by_requests():
jobs = [gevent.spawn(worker, url) for url in urls]
gevent.joinall(jobs)
def by_urllib2():
jobs = [gevent.spawn(worker, url, True) for url in urls]
gevent.joinall(jobs)
if __name__=='__main__':
from timeit import Timer
print 'starting http requests...'
t = Timer(stmt="by_requests()", setup="from __main__ import by_requests")
print 'by requests: %s seconds'%t.timeit(number=3)
t = Timer(stmt="by_urllib2()", setup="from __main__ import by_urllib2")
print 'by urllib2: %s seconds'%t.timeit(number=3)