Skip to content

Commit

Permalink
Workaround to fix ragas: apply nest asyncio only after server forking
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriochaves committed Jan 8, 2025
1 parent f8d5c7d commit 6fefd56
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions langevals/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
from pydantic import BaseModel, ConfigDict, Field, ValidationError
from mangum import Mangum

import nest_asyncio

nest_asyncio_apply = nest_asyncio.apply
nest_asyncio.apply = lambda: None


def handle_sigterm(signum, frame):
print("Received SIGTERM")
Expand Down Expand Up @@ -80,6 +85,8 @@ class Request(BaseModel):
async def evaluate(
req: Request,
) -> List[result_type | EvaluationResultSkipped | EvaluationResultError]: # type: ignore
if module_name == "ragas":
nest_asyncio_apply()
os.environ.clear()
os.environ.update(
original_env
Expand Down Expand Up @@ -135,6 +142,10 @@ def main():
return
import gunicorn.app.base

host = "0.0.0.0"
port = int(os.getenv("PORT", 8000))
workers = get_cpu_count()

class StandaloneApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
Expand All @@ -151,12 +162,9 @@ def load_config(self):
self.cfg.set(key.lower(), value) # type: ignore

def load(self):
print(f"LangEvals listening at http://{host}:{port}")
return self.application

host = "0.0.0.0"
port = int(os.getenv("PORT", 8000))
workers = get_cpu_count()

print(f"Starting server with {workers} workers")

options = {
Expand All @@ -165,6 +173,7 @@ def load(self):
"worker_class": "uvicorn.workers.UvicornWorker",
"preload_app": True,
"forwarded_allow_ips": "*",
"loglevel": "warning",
}

StandaloneApplication(app, options).run()
Expand Down

0 comments on commit 6fefd56

Please sign in to comment.