Created
March 30, 2013 00:11
-
-
Save jxq0/5274574 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
from gevent import monkey | |
monkey.patch_all() | |
import gevent | |
from gevent.wsgi import WSGIServer | |
from gevent.pool import Pool | |
import gevent.socket | |
from cgi import parse_qs, escape | |
import json | |
import redis | |
p = redis.ConnectionPool() | |
def app(environ, start_response): | |
status = '200 OK' | |
body = '' | |
path = environ.get('PATH_INFO', '').lstrip('/') | |
parameters = parse_qs(environ.get('QUERY_STRING', '')) | |
r = redis.Redis(connection_pool=p) | |
if path == 'top': | |
l = r.zrevrange('online', 0, 50, True) | |
body = json.dumps({'onlinetime':map(lambda (pid, online): {'pid':pid, 'onlinetime':online}, l)}) + '\n' | |
headers = [ | |
('Content-Type', 'text/html'), | |
('Content-Length', str(len(body))), | |
] | |
print 'in_use_conn:', len(p._in_use_connections), 'created_connections:', p._created_connections | |
start_response(status, headers) | |
yield body | |
def main(): | |
pool = Pool(1000) | |
WSGIServer(('', 7332), app, spawn=pool, log=None).serve_forever() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
move
r = redis.Redis(connection_pool=p)
outside theapp
s code