Created
June 13, 2013 06:36
-
-
Save codemoran/5771637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import time | |
from wsgiref.simple_server import make_server | |
import graphdat | |
def application(environ, start_response): | |
# get the graphdat timer | |
graphdat = environ['graphdat'] | |
# start a timer at the beginning of an operation | |
graphdat.begin("sorting") | |
# Sort and stringifying the environment vars | |
response_body = ['%s: %s' % (key, value) for key, | |
value in sorted(environ.items())] | |
response_body = '\n'.join(response_body) | |
# end a timer when we are done the operation | |
graphdat.end("sorting") | |
# start a new timer for sending the response | |
graphdat.begin("response") | |
status = '200 OK' | |
response_headers = \ | |
[('Content-Type', 'text/plain'), | |
('Content-Length', str(len(response_body)))] | |
start_response(status, response_headers) | |
BLOCKSIZE = 1000 | |
offset = 0 | |
# create a nested timer under the response timer | |
graphdat.begin('offset' + str(offset)) | |
block = response_body[0:BLOCKSIZE] | |
while block: | |
time.sleep(0.5) | |
yield block | |
graphdat.end('offset' + str(offset)) | |
offset += BLOCKSIZE | |
graphdat.begin('offset' + str(offset)) | |
block = response_body[offset:offset + BLOCKSIZE] | |
# the open timers will automatically be closed | |
# when the response is finished | |
# graphdat wraps the application | |
application = graphdat.WSGIWrapper(application) | |
if __name__ == '__main__': | |
server = make_server( | |
'localhost', # The host name. | |
1337, # A port number. | |
application # Our graphdat wrapped application | |
) | |
print 'server starting on http://localhost:1337' | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment