Python 如何以编程方式将callable传递给gunicorn而不是参数
我使用gunicorn实现了以下web应用程序Python 如何以编程方式将callable传递给gunicorn而不是参数,python,python-3.x,gunicorn,Python,Python 3.x,Gunicorn,我使用gunicorn实现了以下web应用程序 @click.command(“运行应用程序”,help=“在gunicorn中启动应用程序”) def run_uwsgi(): """ 在gunicorn中运行项目 """ 导入系统 sys.argv=[“--gunicorn”] 系统参数附加(“-b 0.0.0.0:5000”) sys.argv.append(“myapp.wsgi:application”) WSGIApplication(用法=“%(程序)s[选项][应用程序模块]”)
@click.command(“运行应用程序”,help=“在gunicorn中启动应用程序”)
def run_uwsgi():
"""
在gunicorn中运行项目
"""
导入系统
sys.argv=[“--gunicorn”]
系统参数附加(“-b 0.0.0.0:5000”)
sys.argv.append(“myapp.wsgi:application”)
WSGIApplication(用法=“%(程序)s[选项][应用程序模块]”)。运行()
这将使用gunicorn加速应用程序,根据要求,如何在不使用参数的情况下加速应用程序?有没有办法将sys.argv值分配给gunicorn?我想发布我已经制定的解决方案
@click.command("uwsgi", help="starts application in gunicorn")
def run_uwsgi():
"""
Runs the project in gunicorn
"""
from gunicorn.app.base import Application
import sys
class MyApplication(Application):
"""
Bypasses the class `WSGIApplication` and made it
independent from command line arguments
"""
def init(self, parser, opts, args):
self.cfg.set("default_proc_name", args[0])
# Added this to ensure the application integrity
self.app_uri = "myapp.wsgi:application"
def load_wsgiapp(self):
# This would do the trick
# returns application callable
return application
def load(self):
return self.load_wsgiapp()
sys.argv = ["--gunicorn"]
sys.argv.append(f"-b {os.environ['APP_HOST']}:{os.environ['APP_PORT']}")
# Throws an error if this is missing.
sys.argv.append("myapp.wsgi:application")
MyApplication(usage="%(prog)s [OPTIONS] [APP_MODULE]").run()
我已经直接从
def load_wsgiapp(self):
# This would do the trick
# returns application callable
return application
wsgi.py
application = main.create_app()
但是仍然需要为模块传递一个命令行参数,否则它会抛出一个错误。
如果您使用Nuitka捆绑您的应用程序,您可以将其旋转并与gunicorn一起使用