Add support for Tornado >= 5

This commit is contained in:
Andrew Rabert 2018-08-08 15:42:50 -04:00 committed by Andrew Rabert
parent 3a434d43c3
commit 74aaf7bf01
3 changed files with 19 additions and 3 deletions

View file

@ -1 +1 @@
tornado>=3.1,<5
tornado>=3.1,<6

View file

@ -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

View file

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