用ThreadPoolExecutor来解决,举个例子:
from tornado import gen,web
from tornado.concurrent import run_on_executor
#if in Python2, it need install by pip install futures
from concurrent.futures import ThreadPoolExecutor
from model.Movie import Movie
class IndexHandler(web.RequestHandler):
executor = ThreadPoolExecutor(2)
@
web.asynchronous
@
gen.coroutine
def get(self): #用yield来返回一个迭代器函数
mov = yield self.query_movie()
self.write('llalalal')
self.finish()
@
run_on_executor #此处是耗时的操作
def query_movie(self):
time.sleep(5)
return 'sleep lallal'
此时就是异步非阻塞的实现了(用线程解决的)