鉴于龙卷风处理程序如下:
class MyHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
queue = self.getQueue() # returns a tornado.queues.Queue
_ = yield queue.get()
self.write("whatever")
def on_connection_close(self):
super().on_connection_close()
# DO SOMETHING HERE THAT BREAKS OUT OF THE YIELD UP THERE??
如果连接关闭,我真的不想在队列中保持阻塞,以后错误地取消一个值。我可以设置一种机制来中止被阻止的get()
方法吗?
答案 0 :(得分:1)
Use chain_future
. Chain queue.get
's future with some other future that is in your control (some kind of indicator/flag), then simply resolve flag future on close:
from tornado.concurrent import Future, chain_future
class MyHandler(tornado.web.RequestHandler):
def initialize(self):
# of course you can create that in get as well
self.close_indicator = Future()
@tornado.gen.coroutine
def get(self):
queue = self.getQueue() # returns a tornado.queues.Queue
qget_future = queue.get()
chain_future(self.close_indicator, qget_future)
_ = yield qget_future
self.write("whatever")
def on_connection_close(self):
super().on_connection_close()
# set close indicator by resolving future
# result will be copied to chained future (qget_future)
self.close_indicator.set_result(None)