-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (39 loc) · 1.34 KB
/
app.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.httpserver import HTTPServer
from tornado import options
from settings import settings, db_pool_settings
from url import urls
import tornadoredis
import time
import signal
options.define("port", default=8888, help="http server port", type=int)
class Chat(Application):
def __init__(self):
super(Chat, self).__init__(urls, **settings)
self.connection_pool = tornadoredis.ConnectionPool(**db_pool_settings)
def make_safely_shutdown(server, timeout=5):
io_loop = IOLoop.instance()
def stop_handler(*args, **keywords):
def shutdown():
server.stop()
deadline = time.time() + timeout
def stop_loop():
now = time.time()
if now < deadline:
io_loop.add_timeout(now + 1, stop_loop)
else:
io_loop.stop()
stop_loop()
io_loop.add_callback(shutdown)
signal.signal(signal.SIGTERM, stop_handler)
signal.signal(signal.SIGINT, stop_handler)
if __name__ == "__main__":
options.parse_command_line()
app = Chat()
server = HTTPServer(app)
server.listen(options.options.port)
make_safely_shutdown(server, timeout=1)
IOLoop.current().start()