Created
August 29, 2013 01:18
-
-
Save bigeagle/6373276 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 python2 | |
# -*- coding:utf-8 -*- | |
import tornado.ioloop | |
import tornado.web | |
import tornado.gen | |
import motor | |
import json | |
class PersistantHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
coll = self.settings['_pdb'].test_coll | |
coll.find().to_list(30, callback=self.on_fetch) | |
def on_fetch(self, records, err): | |
for r in records: | |
self.write(json.dumps( | |
{k: v for k, v in r.iteritems() if k != '_id'})) | |
self.write('<br />') | |
self.finish() | |
class GenPersistantHandler(tornado.web.RequestHandler): | |
@tornado.gen.coroutine | |
def get(self): | |
coll = self.settings['_pdb'].test_coll | |
cursor = coll.find() | |
for r in (yield motor.Op(cursor.to_list, 30)): | |
self.write(json.dumps( | |
{k: v for k, v in r.iteritems() if k != '_id'})) | |
self.write('<br />') | |
self.finish() | |
class SyncRequestHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
self.conn = motor.MotorClient().open_sync() | |
db = self.conn['tornado-test'] | |
coll = db.test_coll | |
coll.find().to_list(30, callback=self.on_fetch) | |
def on_fetch(self, records, err): | |
for r in records: | |
self.write(json.dumps( | |
{k: v for k, v in r.iteritems() if k != '_id'})) | |
self.write('<br />') | |
self.finish() | |
self.conn.close() | |
class AsyncRequestHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
motor.MotorClient().open(self.on_open) | |
def on_open(self, conn, err): | |
self.conn = conn | |
db = self.conn['tornado-test'] | |
coll = db.test_coll | |
coll.find().to_list(30, callback=self.on_fetch) | |
def on_fetch(self, records, err): | |
for r in records: | |
self.write(json.dumps( | |
{k: v for k, v in r.iteritems() if k != '_id'})) | |
self.write('<br />') | |
self.finish() | |
self.conn.close() | |
class GenAsyncRequestHandler(tornado.web.RequestHandler): | |
@tornado.gen.coroutine | |
def get(self): | |
_ = yield tornado.gen.Task(motor.MotorClient().open, ) | |
self.conn, err = _.args | |
db = self.conn['tornado-test'] | |
coll = db.test_coll | |
cursor = coll.find() | |
for r in (yield motor.Op(cursor.to_list, 30)): | |
self.write(json.dumps( | |
{k: v for k, v in r.iteritems() if k != '_id'})) | |
self.write('<br />') | |
self.conn.close() | |
self.finish() | |
if __name__ == "__main__": | |
db = motor.MotorClient().open_sync()['tornado-test'] | |
application = tornado.web.Application([ | |
(r"/p", PersistantHandler), | |
(r"/gp", GenPersistantHandler), | |
(r"/s", SyncRequestHandler), | |
(r"/a", AsyncRequestHandler), | |
(r"/ga", GenAsyncRequestHandler), | |
], _pdb=db, debug=True) | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() | |
# vim: ts=4 sw=4 sts=4 expandtab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Performance with concurrency of 20:
Concurrency 200:
concurrency 1000:
time per request does not increase much with the increase of concurrency.