一个完整的tornado 优雅重启例子

一个完整的tornado 优雅重启例子

一个完整的tornado 优雅重启例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

try:
    sys.modules['json'] = __import__('ujson')
except ImportError:
    pass

import os
import time
os.environ["TZ"] = "Asia/Shanghai"
time.tzset()

import logging
import time
import signal

import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

__author__ = 'weizs'

# simple grace shutdown app.py with tornado

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")


class Application(tornado.web.Application):
    def __init__(self):

        settings = dict(
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies=True,
            cookie_secret='ssaaswq',
            autoescape=None,
            login_url='/oauth/sign-in',
            # debug=True,
        )

        handlers = [
            (r"/", MainHandler),
        ]

        tornado.web.Application.__init__(self, handlers, **settings)


def sig_handler(sig, frame):
    logging.warning('Caught signal: %s', sig)
    tornado.ioloop.IOLoop.instance().add_callback(shutdown)


def shutdown():
    logging.info('Stopping http server')
    server.stop()

    logging.info('Will shutdown in %s seconds ...', 10)
    io_loop = tornado.ioloop.IOLoop.instance()

    deadline = time.time() + 10

    def stop_loop():
        now = time.time()
        if now < deadline and (io_loop._callbacks or io_loop._timeouts):
            io_loop.add_timeout(now + 1, stop_loop)
        else:
            io_loop.stop()
            logging.info('Shutdown')
    stop_loop()


def main():
    try:
        port = int(sys.argv[1])
    except:
        port = 15003

    global server

    server = tornado.httpserver.HTTPServer(Application(), xheaders=True)
    server.listen(port)

    signal.signal(signal.SIGTERM, sig_handler)
    signal.signal(signal.SIGINT, sig_handler)
    tornado.ioloop.IOLoop.instance().start()

    logging.info("Exit...")

if __name__ == "__main__":
    main()

supervisor 开启几个进程,写个脚本依次重启

Bash:
1
2
3
4
5
6
#!/bin/sh

for i in 1 2
    do supervisorctl restart myapp:myapp$i
done
nginx -s reload

/etc/supervisord.conf 配置:

1
2
3
4
5
6
7
8
9
10
11
[program:myapp]
user=root
command = python application.py 1500%(process_num)d
process_name = %(program_name)s%(process_num)d
numprocs = 2
numprocs_start = 1
stopwaitsecs = 11
directory = /srv/www/myapp
redirect_stderr=true
autostart=true
autorestart=true

本文网址: https://pylist.com/topic/117.html 转摘请注明来源

Suggested Topics

Tornado 搭建基于 WebSocket 的聊天服务

这年头 Python web 框架是有点泛滥了. 下面要介绍的是 facebook 的开源框架 tornado. 这东西比较简单, 而且自带 WebSocket 支持, 可以用它做个简单的聊天室. ...

Tornado 构建一个 Comet 应用

Comet -- 基于 HTTP 长连接、无须在浏览器端安装插件的“服务器推”技术为“Comet”,这里介绍用Tornado 构建一个 Comet 应用的经验。...

Leave a Comment