本文介绍了我怎样才能取消挂asyncronous任务龙卷风,一个超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置是蟒蛇龙卷风服务器,该服务器异步处理与的ThreadPoolExecutor 任务。在某些情况下,任务可能变成无限循环。随着 with_timeout 装饰,我已成功地捕捉到超时异常和错误的结果返回给客户端。问题是,该任务仍然在后台运行。它是如何可能从运行中停止任务的的ThreadPoolExecutor ?或者是有可能取消未来
这里是code能重现问题。运行code。与龙卷风4和concurrent.futures库和去:

So, the only way to abort the method you're running in the background is to actually insert logic into your potentially infinite loop so that it can be aborted when you tell it to. With your example, you could use a threading.Event:

class MainHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(2)

    @run_on_executor
    def test_func(self, event):
        i = 0
        while not event.is_set():
            print i
            i = i + 1

    @tornado.gen.coroutine
    def get(self):
        event = threading.Event()
        future = self.test_func(event)
        try:
            result_search_struct = yield with_timeout(datetime.timedelta(seconds=MAX_WAIT_SECONDS), future )
            self.write({'status' : 0})
            self.finish()
        except Exception, e:
            future.cancel() # Might not work, depending on how busy the Executor is
            event.set()
            self.write({'status' : 100})
            self.finish()

application = tornado.web.Application([
    (r"/test", MainHandler),
])

这篇关于我怎样才能取消挂asyncronous任务龙卷风,一个超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:19
查看更多