From 574d042655b6e1e9dd992764416e064e3a6263a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?XD3000/=E9=AB=98=E7=91=9E=E9=9B=B2?= Date: Fri, 12 Jan 2024 14:30:23 +0800 Subject: [PATCH] #1424:gunicorn can't run in windows --- litellm/proxy/proxy_cli.py | 64 +++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.py index c06ba7d3288..0145db86e39 100644 --- a/litellm/proxy/proxy_cli.py +++ b/litellm/proxy/proxy_cli.py @@ -343,7 +343,10 @@ def run_server( ) try: import uvicorn - import gunicorn.app.base + if os.name == "nt": + pass + else: + import gunicorn.app.base except: raise ImportError( "Uvicorn, gunicorn needs to be imported. Run - `pip 'litellm[proxy]'`" @@ -427,36 +430,39 @@ def run_server( f"\033[1;34mSee all Router/Swagger docs on http://0.0.0.0:{port} \033[0m\n" ) # noqa - # Gunicorn Application Class - class StandaloneApplication(gunicorn.app.base.BaseApplication): - def __init__(self, app, options=None): - self.options = options or {} # gunicorn options - self.application = app # FastAPI app - super().__init__() - - def load_config(self): - # note: This Loads the gunicorn config - has nothing to do with LiteLLM Proxy config - config = { - key: value - for key, value in self.options.items() - if key in self.cfg.settings and value is not None - } - for key, value in config.items(): - self.cfg.set(key.lower(), value) - - def load(self): - # gunicorn app function - return self.application - - gunicorn_options = { - "bind": f"{host}:{port}", - "workers": num_workers, # default is 1 - "worker_class": "uvicorn.workers.UvicornWorker", - "preload": True, # Add the preload flag - } from litellm.proxy.proxy_server import app + if os.name == "nt": + uvicorn.run(app, host=host, port=port) # run uvicorn + else: + import gunicorn.app.base + # Gunicorn Application Class + class StandaloneApplication(gunicorn.app.base.BaseApplication): + def __init__(self, app, options=None): + self.options = options or {} # gunicorn options + self.application = app # FastAPI app + super().__init__() - StandaloneApplication(app=app, options=gunicorn_options).run() # Run gunicorn + def load_config(self): + # note: This Loads the gunicorn config - has nothing to do with LiteLLM Proxy config + config = { + key: value + for key, value in self.options.items() + if key in self.cfg.settings and value is not None + } + for key, value in config.items(): + self.cfg.set(key.lower(), value) + + def load(self): + # gunicorn app function + return self.application + + gunicorn_options = { + "bind": f"{host}:{port}", + "workers": num_workers, # default is 1 + "worker_class": "uvicorn.workers.UvicornWorker", + "preload": True, # Add the preload flag + } + StandaloneApplication(app=app, options=gunicorn_options).run() # Run gunicorn if __name__ == "__main__":