2013-05-31 43 views
1

开始使用@run_on_executor我有以下问题,这个小龙卷风测试:龙卷风:线程没有在协程

class SimpleIOLoopTests(tornado.testing.AsyncTestCase): 
    def setUp(self): 
     super(SimpleIOLoopTests, self).setUp() 

    def test_executor_future(self): 
     self.executor = ThreadPoolExecutor(2) 

     @run_on_executor 
     def wait_and_return_a_value(): 
      time.sleep(2) 
      return 20 

     @coroutine 
     def async_compare(callback): 
      val = yield wait_and_return_a_value() 
      assert_that(val, equal_to(20)) 

      callback() 

     async_compare(self.stop) 
     self.wait() 

点是测试只是循环,直到超时发生。调试代码看起来好像executor-future被创建为done(),因此甚至没有被io_loop启动。

我在这里做错了什么?帮助解决这个问题是非常感谢

顺便说一句:如果我使用@return_future装饰像这样的(为其它是真正的意外是已经完成)

@return_future 
    def get_value(callback): 
     callback(10) 
创建一个普通的未来发生同样的情况

感谢&问候 马库斯

回答

2

问题是,遗嘱执行人必须在该io_loop和执行定义(这可以看出,当您检查@run_on_executor装饰)一类的“活”。

def test_executor_future(self): 
    class Executor(): 
     def __init__(self, io_loop=None): 
      self.io_loop = io_loop or IOLoop.instance() 
      self.executor = ThreadPoolExecutor(2) 

     @tornado.concurrent.run_on_executor 
     def wait_and_return_a_value(self): 
      return 20 

     def destroy(self): 
      self.executor.shutdown(1) 

    @tornado.gen.coroutine 
    def async_compare(callback): 
     executor = Executor() 
     val = yield executor.wait_and_return_a_value() 
     assert_that(val, equal_to(20)) 

     executor.destroy() 
     callback() 

    async_compare(self.stop) 
    self.wait()