このスクリプトのパフォーマンスはどうですか: http://tornadogists.org/2185380/以下にコピーします。
from time import sleep
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, asynchronous, RequestHandler
from multiprocessing.pool import ThreadPool
_workers = ThreadPool(10)
def run_background(func, callback, args=(), kwds={}):
def _callback(result):
IOLoop.instance().add_callback(lambda: callback(result))
_workers.apply_async(func, args, kwds, _callback)
# blocking task like querying to MySQL
def blocking_task(n):
sleep(n)
return n
class Handler(RequestHandler):
@asynchronous
def get(self):
run_background(blocking_task, self.on_complete, (10,))
def on_complete(self, res):
self.write("Test {0}<br/>".format(res))
self.finish()
HTTPServer(Application([("/", Handler)],debug=True)).listen(8888)
IOLoop.instance().start()
- 私のアプリケーションは1,000 req/secをはるかに超えます。
- 各リクエストは 2 ~ 30 秒続き、平均で約 6 秒かかります
- 単純平均
sleep(6)
- 単純平均
redis BLPOP
またはのようなものを使用して IO をブロックします。Queue.get_nowait()