如何以编程方式将可调用对象传递给 gunicorn 而不是参数
How to progammatically pass the callable to gunicorn instead of arguments
我有以下实现来使用 gunicorn 启动网络应用程序
@click.command("run_app", help="starts application in gunicorn")
def run_uwsgi():
"""
Runs the project in gunicorn
"""
import sys
sys.argv = ["--gunicorn"]
sys.argv.append("-b 0.0.0.0:5000")
sys.argv.append("myapp.wsgi:application")
WSGIApplication(usage="%(prog)s [OPTIONS] [APP_MODULE]").run()
这将根据要求使用 gunicorn 启动应用程序如何在不使用参数的情况下启动它?有没有办法为 gunicorn 分配 sys.argv 值?
我想要post我已经制定的解决方案
@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 一起使用。
我有以下实现来使用 gunicorn 启动网络应用程序
@click.command("run_app", help="starts application in gunicorn")
def run_uwsgi():
"""
Runs the project in gunicorn
"""
import sys
sys.argv = ["--gunicorn"]
sys.argv.append("-b 0.0.0.0:5000")
sys.argv.append("myapp.wsgi:application")
WSGIApplication(usage="%(prog)s [OPTIONS] [APP_MODULE]").run()
这将根据要求使用 gunicorn 启动应用程序如何在不使用参数的情况下启动它?有没有办法为 gunicorn 分配 sys.argv 值?
我想要post我已经制定的解决方案
@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 一起使用。