You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

import os
import signal
import platform
import asyncio
import tornado.ioloop
import tornado.netutil
import tornado.httpserver
def load_env(key, default):
if key in os.environ:
return os.environ[key]
return default
signal_received = False
def platform_setup():
"""Platform-specific setup, especially for asyncio."""
if platform.system() == 'Windows':
def run_tornado_server(app, server_port=8080):
sockets = tornado.netutil.bind_sockets(server_port, '')
server = tornado.httpserver.HTTPServer(app)
port = None
for s in sockets:
print('Listening on %s, port %d' % s.getsockname()[:2])
if port is None:
port = s.getsockname()[1]
ioloop = tornado.ioloop.IOLoop.instance()
def register_signal(sig, _frame):
# noinspection PyGlobalUndefined
global signal_received
print("%s received, stopping server" % sig)
server.stop() # no more requests are accepted
signal_received = True
def stop_on_signal():
# noinspection PyGlobalUndefined
global signal_received
if signal_received:
print("IOLoop stopped")
tornado.ioloop.PeriodicCallback(stop_on_signal, 1000).start()
signal.signal(signal.SIGTERM, register_signal)
print("Starting server")
global signal_received
while not signal_received:
except KeyboardInterrupt:
print("Keyboard interrupt")
register_signal(signal.SIGTERM, None)