Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何以编程方式将callable传递给gunicorn而不是参数_Python_Python 3.x_Gunicorn - Fatal编程技术网

Python 如何以编程方式将callable传递给gunicorn而不是参数

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[选项][应用程序模块]”)

我使用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[选项][应用程序模块]”)。运行()

这将使用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一起使用