-
Notifications
You must be signed in to change notification settings - Fork 19
[tornado] multi process
Myungchul Shin edited this page Feb 1, 2017
·
3 revisions
debug 값이 True인 경우는
autoreload
때문에 multi process로 실행이 안된다.
따라서, debug값에 따라서 single, multi로 분기 실행되도록 해야한다.
import tornado.web
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.process
import tornado.autoreload as autoreload
from tornado.options import define, options
class Application(tornado.web.Application):
def __init__(self):
settings = dict(
static_path = os.path.join(os.path.dirname(__file__), 'static'),
template_path = os.path.join(os.path.dirname(__file__), 'templates'),
autoescape = None,
debug = options.debug,
gzip = True
)
handlers = [
(r'/', IndexHandler),
]
tornado.web.Application.__init__(self, handlers, **settings)
autoreload.add_reload_hook(self.finalize)
.....
def main():
tornado.options.parse_command_line()
application = Application()
httpServer = tornado.httpserver.HTTPServer(application, no_keep_alive=True)
if options.debug == True :
httpServer.listen(options.port)
else :
httpServer.bind(options.port)
httpServer.start(0) # Forks multiple sub-processes
.....
tornado.ioloop.IOLoop.instance().start()
....
log의 경우는 mongodb를 사용하는 방법을 참고해서, mongodb로 바로 저장하는 방법을 사용하자.
log = logging.getLogger('tornado.application')
def setupMongoLogger() :
log.addHandler(MongoHandler.to(db='mongolog', collection='log', host='host:port'))
return log
class Application(tornado.web.Application):
def __init__(self):
...
self.log = setupMongoLogger()
# overrided
def log_request(self, handler):
if handler.get_status() :
log_method = log.info
elif handler.get_status() :
log_method = log.warn
else:
log_method = log.error
request_time = 1000.0 * handler.request.request_time()
log_message = '%d %s %.2fms' % (handler.get_status(), handler._request_summary(), request_time)
log_method(log_message)
class QHandler(BaseHandler):
def get(self) :
...
self.log.info(ret)
self.write(ret)
self.finish()