3
# Copyright (c) 2012 New Dream Network, LLC (DreamHost)
5
Use the Greenlet class to run multiple "jobs" and wait for them to complete.
8
from gevent import monkey
18
def get_page(url, content_offset=100):
20
data = urllib2.urlopen(url).read()
21
except Exception, err:
26
def check_content(greenlet_exit):
27
data = greenlet_exit.value
29
if isinstance(data, basestring):
32
def create_greenlets(urls):
35
g = gevent.Greenlet(get_page, url, content_offset=20)
36
g.link_value(check_content)
42
def complex(url_count=1):
43
urls = ["http://127.0.0.1"] * url_count
44
jobs = create_greenlets(urls)
48
def simple(url_count=1):
49
urls = ["http://127.0.0.1"] * url_count
50
jobs = [gevent.spawn(get_page, url) for url in urls]
54
def run_it(runs, requests):
55
for command in ['complex', 'simple']:
56
import_string = 'from __main__ import %s' % command
57
command_string = '%s(url_count=%s)' % (command, requests)
59
time = timeit.timeit(command_string, import_string, number=run)
60
print "%s, %s, %s, %s" % (command, run, requests, time)
62
def run_it_2(runs, requests):
63
for command in ['complex', 'simple']:
64
import_string = 'from __main__ import %s' % command
65
for run, request_count in zip(runs, requests):
66
command_string = '%s(url_count=%s)' % (command, request_count)
67
time = timeit.timeit(command_string, import_string, number=run)
68
print "%s, %s, %s, %s" % (command, run, request_count, time)
72
runs = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,100,200,500,1000,5000,10000]
74
#run_it(runs, requests)
76
runs = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,100,200,500,1000]
78
#run_it(runs, requests)
80
runs = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,100]
82
#run_it(runs, requests)
84
runs = [1,2,3,4,5,6,7,8,9,10]
86
#run_it(runs, requests)
90
#run_it(runs, requests)
94
#run_it(runs, requests)
96
runs = [100000,10000,1000,100,10,1]
97
requests = [1,10,100,1000,10000,100000]
98
run_it_2(runs, requests)