Skip to content

Instantly share code, notes, and snippets.

@Ethan-Zhang
Created November 18, 2013 08:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ethan-Zhang/7524628 to your computer and use it in GitHub Desktop.
Save Ethan-Zhang/7524628 to your computer and use it in GitHub Desktop.
When using tornado multi process deploy, the log handler TimedRotatingFileHandler will cause exception when the log file split. Because one of the process rename the log file and create a new log file, then the log file descriptor changed. So, we make every process has its own log file, and name it with the process id.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2012 Ethan Zhang<http://github.com/Ethan-Zhang>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import signal
import os
import logging
import logging.handlers
import tornado
import tornado.netutil
import tornado.process
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
def handle_request(request):
message = "You requested %s\n" % request.uri
request.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (
len(message), message))
request.finish()
def kill_server(sig, frame):
IOLoop.instance().stop()
def main():
signal.signal(signal.SIGPIPE, signal.SIG_IGN);
signal.signal(signal.SIGINT, kill_server)
signal.signal(signal.SIGQUIT, kill_server)
signal.signal(signal.SIGTERM, kill_server)
signal.signal(signal.SIGHUP, kill_server)
sockets = tornado.netutil.bind_sockets(9204)
task_id = tornado.process.fork_processes(2)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
log_file = 'tornadolog.log.%d' % (task_id)
timelog = timelog = logging.handlers.TimedRotatingFileHandler(log_file,
'midnight',
1, 0)
logger.addHandler(timelog)
server = HTTPServer(handle_request)
server.add_sockets(sockets)
logger.info('Server Starting...')
IOLoop.instance().start()
server.stop()
IOLoop.instance().stop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment