-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathstart_fastapi_server.py
More file actions
60 lines (47 loc) · 1.76 KB
/
start_fastapi_server.py
File metadata and controls
60 lines (47 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Starts the Gateway Service.
You can do this with `start-fastapi-server`.
"""
import argparse
import os
import subprocess
from typing import List
def start_gunicorn_server(port: int, num_workers: int, debug: bool) -> None:
"""Starts a GUnicorn server locally."""
additional_args: List[str] = []
if debug:
additional_args.extend(["--reload", "--timeout", "0"])
# Use environment variables for configuration with fallbacks
timeout = int(os.environ.get('WORKER_TIMEOUT', os.environ.get('GUNICORN_TIMEOUT', 60)))
graceful_timeout = int(os.environ.get('GUNICORN_GRACEFUL_TIMEOUT', timeout))
keep_alive = int(os.environ.get('GUNICORN_KEEP_ALIVE', 2))
worker_class = os.environ.get('GUNICORN_WORKER_CLASS', 'model_engine_server.api.worker.LaunchWorker')
command = [
"gunicorn",
"--bind",
f"[::]:{port}",
"--timeout",
str(timeout),
"--graceful-timeout",
str(graceful_timeout),
"--keep-alive",
str(keep_alive),
"--worker-class",
worker_class,
"--workers",
f"{num_workers}",
*additional_args,
"model_engine_server.api.app:app",
]
subprocess.run(command, check=True)
def entrypoint():
"""Entrypoint for starting a local server."""
# We can probably use asyncio since this service is going to be more I/O bound.
parser = argparse.ArgumentParser(description="Hosted Inference Server")
parser.add_argument("--port", type=int, default=5000)
parser.add_argument("--num-workers", type=int, default=4)
parser.add_argument("--debug", "-d", action="store_true")
args = parser.parse_args()
start_gunicorn_server(args.port, args.num_workers, args.debug)
if __name__ == "__main__":
entrypoint()