crx349 发表于 2024-7-14 18:43:03

pyinstaller打包gunicorn启动的Flask

app.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from hello import hallo
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'hello world'


application = app

#以gunicorn 启动的Flask不会走main方法所以下面的可以注释掉

if __name__=="__main__":
    app.run()

gunicorn.py
#!/usr/bin/env python3

from gunicorn.app.base import BaseApplication

class Application(BaseApplication):
    def load_config(self):
      s = self.cfg.set
      s('bind', "0.0.0.0:18000")
      s('workers', 1)
      s('timeout', 30)
      s('accesslog', "/opt/xmspace/log/gunicorn_access.log")
      s('errorlog', "/opt/xmspace/log/gunicorn_error.log")
      s('access_log_format', '%(t)s %(h)s "%(r)s" %(s)s %(b)s %(D)s "%(a)s"')

    def load(self):
      from app import application
      return application

if __name__ == '__main__':
    Application().run()

ssh下执行
pyinstaller -F gunicorn.py --name gun--hidden-import gunicorn.glogging --hidden-import gunicorn.workers.sync

flask默认启动是单线程 gunicorn启动好处就是多线程多并发
页: [1]
查看完整版本: pyinstaller打包gunicorn启动的Flask