请教下: 假设我需要执行两次获取数据的操作, 分别需要耗时 5 秒和 10 秒, 也需要等待返回结果才继续处理, 要怎么弄?
class GetDataHandler(BaseHandler):
executor = ThreadPoolExecutor(2)
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
t1 = self.executor.submit(self.get_something, self, 'url-1')
t2 = self.executor.submit(self.get_something, self, 'url-2')
data_1 = yield t1.result()
data_2 = yield t2.result()
...
self.write('OK')
@run_on_executor
def get_something(self, url):
print('start...')
data = requests.get(url)
return data
堵倒是不堵了, 也可以同时开始, 但返回结果就要 15 秒的样? 这个 yield 还是有点蒙