gunicorn 如何通过代码启动?

gunicorn 如何通过代码启动?

使用 uvicorn 的时候,可以通过 import 导包的方式使用:

main.py

import uvicorn

import settings

import psutil

from api import create_app

app = create_app()

if __name__ == "__main__":

worker_num = (

settings.ASGI_CONFIG.workers

if settings.ASGI_CONFIG.workers > 0

else psutil.cpu_count(logical=False)

)

uvicorn.run(

app='main:app',

host="0.0.0.0",

port=settings.ASGI_CONFIG.port,

workers=worker_num,

)

然后通过下面的方式启动:

python main.py          

但是,我没有找到 gunicorn 这样的用法:
api.py

from werkzeug.wrappers import Request, Response

class MovieApp(object):

"""Implements a WSGI application for managing your favorite movies."""

def __init__(self):

pass

def dispatch_request(self, request):

"""Dispatches the request."""

return Response('Hello World!')

def wsgi_app(self, environ, start_response):

"""WSGI application that processes requests and returns responses."""

request = Request(environ)

response = self.dispatch_request(request)

return response(environ, start_response)

def __call__(self, environ, start_response):

"""The WSGI server calls this method as the WSGI application."""

return self.wsgi_app(environ, start_response)

def create_app():

"""Application factory function that returns an instance of MovieApp."""

app = MovieApp()

return app

if __name__ == '__main__':

from gunicorn.app.wsgiapp import WSGIApplication

app = create_app()

WSGIApplication('api:app').run()

无法启动!

─➤  python api.py                                           

Error: No application module specified.

不想通过 cmd :gunicorn main:app 启动


回答:

可以这样比如:python -m gunicorn.app.wsgiapp -c /gunicorn.conf.py server.main:app

但还是要写额外参数。你应该是想快速启动记不住命令吧,那建议你用工具pycharm里面有运行配置设置好,一点就启动了,快的不行

以上是 gunicorn 如何通过代码启动? 的全部内容, 来源链接: utcz.com/p/938589.html

回到顶部