Quellcode durchsuchen

[update] webAPI集成Gunicorn

jhao vor 7 Jahren
Ursprung
Commit
9a37bbc8fb
2 geänderte Dateien mit 37 neuen und 4 gelöschten Zeilen
  1. 35 4
      Api/ProxyApi.py
  2. 2 0
      requirements.txt

+ 35 - 4
Api/ProxyApi.py

@@ -3,17 +3,20 @@
 """
 -------------------------------------------------
    File Name:     ProxyApi.py
-   Description :
+   Description :   WebApi
    Author :       JHao
    date:          2016/12/4
 -------------------------------------------------
    Change Activity:
-                   2016/12/4:
+                   2016/12/04: WebApi
+                   2019/08/14: 集成Gunicorn启动方式
 -------------------------------------------------
 """
 __author__ = 'JHao'
 
 import sys
+import gunicorn.app.base
+from gunicorn.six import iteritems
 from werkzeug.wrappers import Response
 from flask import Flask, jsonify, request
 
@@ -83,9 +86,37 @@ def getStatus():
     return status
 
 
-def run():
+class StandaloneApplication(gunicorn.app.base.BaseApplication):
+
+    def __init__(self, app, options=None):
+        self.options = options or {}
+        self.application = app
+        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
+
+
+def runFlask():
     app.run(host=config.host_ip, port=config.host_port)
 
 
+def runFlaskWithGunicorn():
+    _options = {
+        'bind': '%s:%s' % (config.host_ip, config.host_port),
+        'workers': 4,
+        'accesslog': '-',  # log to stdout
+        'access_log_format': '%(h)s %(l)s %(t)s "%(r)s" %(s)s "%(a)s"'
+    }
+    StandaloneApplication(app, _options).run()
+
+
 if __name__ == '__main__':
-    run()
+    # runFlask()
+    runFlaskWithGunicorn()

+ 2 - 0
requirements.txt

@@ -4,5 +4,7 @@ Flask==0.12
 requests==2.20.0
 lxml==3.7.2
 PyExecJS==1.5.1
+click==7.0
+gunicorn==19.9.0
 pymongo
 redis