Skip to content

Instantly share code, notes, and snippets.

@jxq0
Created March 30, 2013 00:11
Show Gist options
  • Save jxq0/5274574 to your computer and use it in GitHub Desktop.
Save jxq0/5274574 to your computer and use it in GitHub Desktop.
#!/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()
@jmb0z
Copy link

jmb0z commented Nov 9, 2016

move r = redis.Redis(connection_pool=p) outside the apps code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment