一个完整的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

you-get 优酷完整视频

you-get 是个很好的项目,可以用来下载多个网站的视频,但下载优酷视频时往往只能下载前面一部分视频,下面是介绍下载优酷全部视屏的方法。...

Leave a Comment