# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license. 
# See the NOTICE for more information.

import os

from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop, PeriodicCallback

from gunicorn.workers.base import Worker

class TornadoWorker(Worker):
    
    def watchdog(self):
        self.notify()
            
        if self.ppid != os.getppid():
            self.log.info("Parent changed, shutting down: %s" % self)
            self.ioloop.stop()
    
    def run(self):
        self.socket.setblocking(0)
        self.ioloop = IOLoop.instance()
        PeriodicCallback(self.watchdog, 1000, io_loop=self.ioloop).start()

        server = HTTPServer(self.app, io_loop=self.ioloop)
        server._socket = self.socket
        server.start(num_processes=1)

        self.ioloop.start()

@@ -131,16 +131,27 @@
 mport os
+%0Aimport sys
 %0A%0Afrom t
@@ -239,16 +239,17 @@
 llback%0A%0A
+%0A
 from gun
@@ -285,38 +285,473 @@
 ker%0A
-%0Aclass TornadoWorker(Worker):%0A
+from gunicorn import __version__ as gversion%0A%0A%0Adef patch_request_handler():%0A    web = sys.modules.pop(%22tornado.web%22)%0A%0A    old_clear = web.RequestHandler.clear%0A%0A    def clear(self):%0A        old_clear(self)%0A        self._headers%5B%22Server%22%5D += %22 (Gunicorn/%25s)%22 %25 gversion%0A         %0A    web.RequestHandler.clear = clear%0A    sys.modules%5B%22tornado.web%22%5D = web%0A    %0A%0Aclass TornadoWorker(Worker):%0A    %0A    @classmethod%0A    def setup(cls):%0A        patch_request_handler()%0A    
     

patch RequestHandler so people know that they use tornado with gunicorn.