- import gevent
- import tornado.ioloop
- import tornado.web
- import amysql
- from random import randint
- con = amysql.Connection()
- con.connect ('127.0.0.1', 3306, 'root', '123', 'test')
- con.query('SET GLOBAL query_cache_size = 0;')
- def query(r):
- return con.query('SELECT value FROM test WHERE id >= %s LIMIT 10;', (r,))
- class MainHandler(tornado.web.RequestHandler):
- @tornado.web.asynchronous
- def get(self):
- self.queries = queries = []
- for i in xrange(10):
- r = randint(1, 1000000 - 10)
- queries.append(gevent.spawn(query, r))
- tornado.ioloop.IOLoop.instance().add_callback(self.output) # 或者add_timeout(time.time() + 0.1, self.output)
- def output(self):
- sum = 0
- for q in self.queries:
- rs = q.get()
- for j in rs.rows:
- sum += j[0]
- self.write('%6f' % sum)
- self.finish()
- application = tornado.web.Application([
- (r"/", MainHandler),
- ])
- if __name__ == "__main__":
- application.listen(8000)
- tornado.ioloop.IOLoop.instance().start()
setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
阅读(928) | 评论(0) | 转发(0) |