Skip to content

Instantly share code, notes, and snippets.

@jdmaturen
Created January 16, 2011 00:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdmaturen/781388 to your computer and use it in GitHub Desktop.
Save jdmaturen/781388 to your computer and use it in GitHub Desktop.
from gevent import monkey; monkey.patch_socket()
import gevent
import httplib2
from gevent import http
from time import time
def callback(request):
st = time()
print request
request.add_output_header('Content-Type', 'text/plain')
request.send_reply_start(200, "OK")
request.send_reply_chunk("Hello World %dms\n" % (1000*(time()-st)))
h = httplib2.Http()
urls = ['http://localhost:9001/20', 'http://localhost:9001/10', 'http://localhost:9001/1']
for url in urls:
t = time()
resp, content = h.request(url)
dt = time()-t
request.send_reply_chunk("%s %s %dms, %dms elapsed\n" % (url, resp is not None,
1000*dt, 1000*(time()-st)))
request.send_reply_end()
print 'Serving on 9000...'
http.HTTPServer(('127.0.0.1', 9000), callback).serve_forever()
from gevent import monkey; monkey.patch_socket()
import gevent
import httplib2
from gevent import http
from gevent import queue
from time import time
def worker(q):
h = httplib2.Http()
while True:
url, return_q = q.get()
print 'getting', url
t = time()
resp = content = None
with gevent.Timeout(0.2, False):
resp, content = h.request(url, "GET")
if resp is None:
h = httplib2.Http()
return_q.put((url, resp, content, time()-t))
def callback(request):
global req_q
st = time()
print request
request.add_output_header('Content-Type', 'text/plain')
request.send_reply_start(200, "OK")
request.send_reply_chunk("Hello World %dms\n" % (1000*(time()-st)))
urls = ['http://localhost:9001/20', 'http://localhost:9001/10', 'http://localhost:9001/1']
rsp_q = queue.Queue()
for url in urls:
req_q.put((url, rsp_q))
count = len(urls)
while count:
url, resp, content, dt = rsp_q.get()
request.send_reply_chunk("%s %s %dms, %dms elapsed\n" % (url, resp is not None,
1000*dt, 1000*(time()-st)))
count -= 1
request.send_reply_end()
req_q = queue.Queue()
workers = 64*4
for i in xrange(0, workers):
gevent.spawn(worker, req_q)
print 'Serving on 9000...'
http.HTTPServer(('127.0.0.1', 9000), callback).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment