diff --git a/requires/installation.txt b/requires/installation.txt index 07af68f..e0dcd27 100644 --- a/requires/installation.txt +++ b/requires/installation.txt @@ -1 +1 @@ -tornado>=3.1,<5 +tornado>=3.1,<6 diff --git a/sprockets/http/app.py b/sprockets/http/app.py index 09bb19d..54794ec 100644 --- a/sprockets/http/app.py +++ b/sprockets/http/app.py @@ -2,6 +2,11 @@ import logging import sys from tornado import concurrent, web +import tornado + +ASYNCIO_TORNADO = tornado.version_info[0] >= 5 +if ASYNCIO_TORNADO: + import asyncio class _ShutdownHandler(object): @@ -40,13 +45,18 @@ class _ShutdownHandler(object): def _maybe_stop(self): now = self.io_loop.time() - if (now < self.__deadline and - (self.io_loop._callbacks or self.io_loop._timeouts)): + if now < self.__deadline and self._ioloop_has_tasks(): self.io_loop.add_timeout(now + 1, self._maybe_stop) else: self.io_loop.stop() self.logger.info('stopped IOLoop') + def _ioloop_has_tasks(self): + if ASYNCIO_TORNADO: + return bool(asyncio.Task.all_tasks()) + else: + return bool(self.io_loop._callbacks or self.io_loop._timeouts) + class CallbackManager(object): """ @@ -120,6 +130,10 @@ class CallbackManager(object): for callback in self.on_shutdown_callbacks: try: maybe_future = callback(self.tornado_application) + + if ASYNCIO_TORNADO and asyncio.iscoroutine(maybe_future): + maybe_future = asyncio.create_task(maybe_future) + if concurrent.is_future(maybe_future): shutdown.add_future(maybe_future) running_async = True diff --git a/tests.py b/tests.py index b37e58c..0221508 100644 --- a/tests.py +++ b/tests.py @@ -448,6 +448,7 @@ class RunnerTests(MockHelper, unittest.TestCase): self.io_loop.add_callback_from_signal.assert_called_once_with( runner._shutdown) + @unittest.skipUnless(tornado.version_info < (5,), '') def test_that_shutdown_waits_for_callbacks(self): def add_timeout(_, callback): self.io_loop._callbacks.pop() @@ -461,6 +462,7 @@ class RunnerTests(MockHelper, unittest.TestCase): self.io_loop.stop.assert_called_once_with() self.assertEqual(self.io_loop.add_timeout.call_count, 2) + @unittest.skipUnless(tornado.version_info < (5,), '') def test_that_shutdown_waits_for_timeouts(self): def add_timeout(_, callback): self.io_loop._timeouts.pop()