Skip to content

Commit

Permalink
tornado 5 support (#1918)
Browse files Browse the repository at this point in the history
 tornado 5 support by keeping track of the periodic callbacks
  • Loading branch information
bivald authored and benoitc committed Dec 9, 2018
1 parent ba9a446 commit 33025cf
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions gunicorn/workers/gtornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
from gunicorn import __version__ as gversion


# `io_loop` arguments to many Tornado functions have been removed in Tornado 5.0
# <http://www.tornadoweb.org/en/stable/releases/v5.0.0.html#backwards-compatibility-notes>
IOLOOP_PARAMETER_REMOVED = tornado.version_info >= (5, 0, 0)
# Tornado 5.0 updated its IOLoop, and the `io_loop` arguments to many
# Tornado functions have been removed in Tornado 5.0. Also, they no
# longer store PeriodCallbacks in ioloop._callbacks. Instead we store
# them on our side, and use stop() on them when stopping the worker.
# See https://www.tornadoweb.org/en/stable/releases/v5.0.0.html#backwards-compatibility-notes
# for more details.
TORNADO5 = tornado.version_info >= (5, 0, 0)


class TornadoWorker(Worker):
Expand Down Expand Up @@ -66,8 +70,13 @@ def heartbeat(self):
pass
self.server_alive = False
else:
if not self.ioloop._callbacks:
if TORNADO5:
for callback in self.callbacks:
callback.stop()
self.ioloop.stop()
else:
if not self.ioloop._callbacks:
self.ioloop.stop()

def init_process(self):
# IOLoop cannot survive a fork or be shared across processes
Expand All @@ -81,9 +90,13 @@ def run(self):
self.ioloop = IOLoop.instance()
self.alive = True
self.server_alive = False
if IOLOOP_PARAMETER_REMOVED:
PeriodicCallback(self.watchdog, 1000).start()
PeriodicCallback(self.heartbeat, 1000).start()

if TORNADO5:
self.callbacks = []
self.callbacks.append(PeriodicCallback(self.watchdog, 1000))
self.callbacks.append(PeriodicCallback(self.heartbeat, 1000))
for callback in self.callbacks:
callback.start()
else:
PeriodicCallback(self.watchdog, 1000, io_loop=self.ioloop).start()
PeriodicCallback(self.heartbeat, 1000, io_loop=self.ioloop).start()
Expand Down Expand Up @@ -127,13 +140,13 @@ def on_close(instance, server_conn):
# options
del _ssl_opt["do_handshake_on_connect"]
del _ssl_opt["suppress_ragged_eofs"]
if IOLOOP_PARAMETER_REMOVED:
if TORNADO5:
server = server_class(app, ssl_options=_ssl_opt)
else:
server = server_class(app, io_loop=self.ioloop,
ssl_options=_ssl_opt)
else:
if IOLOOP_PARAMETER_REMOVED:
if TORNADO5:
server = server_class(app)
else:
server = server_class(app, io_loop=self.ioloop)
Expand Down

0 comments on commit 33025cf

Please sign in to comment.