Skip to content

Commit

Permalink
Merge 9b9a532 into cdf4d51
Browse files Browse the repository at this point in the history
  • Loading branch information
mjrk committed Apr 20, 2017
2 parents cdf4d51 + 9b9a532 commit a983717
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion supercell/requesthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import json
import logging
import time
import inspect

from schematics.models import Model
from schematics.exceptions import BaseError
Expand Down Expand Up @@ -224,7 +225,7 @@ def _execute(self, transforms, *args, **kwargs):

method = getattr(self, self.request.method.lower())
result = method(*self.path_args, **self.path_kwargs)
if is_future(result):
if is_future(result) or inspect.iscoroutinefunction(method):
result = yield result
if result is not None:
self._provide_result(verb, headers, result)
Expand Down
15 changes: 14 additions & 1 deletion supercell/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def shutdown(self):

def stop_loop():
now = time.time()
if now < dl and (io_loop._callbacks or io_loop._timeouts):
if now < dl and self._has_callbacks(io_loop):
io_loop.add_timeout(now + 1, stop_loop)
else:
io_loop.stop()
Expand Down Expand Up @@ -286,3 +286,16 @@ def run(self):
"""Implement this method in order to add handlers and managed objects
to the environment, before the app is started."""
pass

def _has_callbacks(self, io_loop):
"""Check if the io_loop has pending callbacks.
The check works with the default io_loop as well as the asyncio loop.
You may have to override it for other implemations.
"""
if hasattr(tornado.platform, "asyncio") and isinstance(
io_loop, tornado.platform.asyncio.AsyncIOMainLoop
):
return io_loop.asyncio_loop._scheduled
else:
return (io_loop._callbacks or io_loop._timeouts)
2 changes: 2 additions & 0 deletions test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ def test_graceful_shutdown_pending_callbacks(self, ioloop_instance_mock):

if sys.version_info < (3,):
expected.extend([mock.call(), mock.call().remove_handler(mock.ANY),
mock.call()._callbacks.__nonzero__(),
mock.call()._callbacks.__nonzero__(),
mock.call().add_timeout(mock.ANY, mock.ANY)])
else:
expected.extend([mock.call(), mock.call().remove_handler(mock.ANY),
mock.call()._callbacks.__bool__(),
mock.call().add_timeout(mock.ANY, mock.ANY)])

assert expected == ioloop_instance_mock.mock_calls

@mock.patch('tornado.ioloop.IOLoop.instance')
Expand Down

0 comments on commit a983717

Please sign in to comment.