forked from lalala223/flask-restful-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
73 lines (58 loc) · 1.99 KB
/
manager.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
from gevent import monkey; monkey.patch_all()
import logging.handlers
from abc import ABC
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from gunicorn.app.base import BaseApplication
from gunicorn.six import iteritems
from multiprocessing import cpu_count
from app import create_app, db
from config import config
app = create_app(config)
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
class StandaloneApplication(BaseApplication, ABC):
"""
gunicorn服务器启动类
"""
def __init__(self, application, options):
self.application = application
self.options = options or {}
super(StandaloneApplication, self).__init__()
def load_config(self):
config = dict([(key, value) for key, value in iteritems(self.options)
if key in self.cfg.settings and value is not None])
for key, value in iteritems(config):
self.cfg.set(key.lower(), value)
def load(self):
return self.application
@manager.command
def run():
"""
生产模式启动命令函数
To use: python3 manager.py run
"""
app.logger.setLevel(app.config.get('LOG_LEVEL', logging.INFO))
service_config = {
'bind': app.config.get('BIND', '0.0.0.0:5000'),
'workers': app.config.get('WORKERS', cpu_count() * 2 + 1),
'worker_class': 'gevent',
'worker_connections': app.config.get('WORKER_CONNECTIONS', 10000),
'backlog': app.config.get('BACKLOG', 2048),
'timeout': app.config.get('TIMEOUT', 60),
'loglevel': app.config.get('LOG_LEVEL', 'info'),
'pidfile': app.config.get('PID_FILE', 'run.pid'),
}
StandaloneApplication(app, service_config).run()
@manager.command
def debug():
"""
debug模式启动命令函数
To use: python3 manager.py debug
"""
app.logger.setLevel(logging.DEBUG)
app.run(debug=True)
if __name__ == '__main__':
manager.run()