Question

I have the following problem with this little tornado-test:

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()

The point is the the test simply loops until a timeout occurs. Debugging the code it looks as if the executor-future is created as done() and, hence, no even started by the io_loop.

What am I doing wrong here? Help with this issue is really appreciated

btw: the same happens if I create a trivial future using the @return_future decorator like this one (for which the it is accidently true that is is already done)

@return_future
    def get_value(callback):
        callback(10)

thanks & regards markus

Was it helpful?

Solution

Problem is that executors must "live" in a class where the io_loop and the executor is defined (this can be seen when you check the @run_on_executor decorator).

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top