Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django + Tornado example
#!/usr/bin/python
import django.core.handlers.wsgi
import os, signal, functools, time
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
from tornado.options import options, define
try:
define("port", default=8888, help="run on the given port", type=int)
except: pass
try:
define("log_file_prefix", type=str, default=None, metavar="PATH",
help=("Path prefix for log files. "
"Note that if you are running multiple tornado processes, "
"log_file_prefix must be different for each of them (e.g. "
"include the port number)"))
except: pass
def stop_server(server, loop):
server.stop()
loop.add_timeout(time.time() + 5.0, functools.partial(stop_loop, loop))
def stop_loop(loop):
loop.stop()
def sighup_handler(http_server, ioloop, signum, frame):
ioloop.add_callback(functools.partial(stop_server, http_server, ioloop))
if __name__ == "__main__":
tornado.options.parse_command_line()
os.environ["DJANGO_SETTINGS_MODULE"] = 'settings'
application = django.core.handlers.wsgi.WSGIHandler()
container = tornado.wsgi.WSGIContainer(application)
http_server = tornado.httpserver.HTTPServer(container, xheaders=True)
ioloop = tornado.ioloop.IOLoop.instance()
signal.signal(signal.SIGHUP, lambda signum, frame: sighup_handler(http_server, ioloop, signum, frame))
http_server.listen(options.port)
ioloop.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.