Skip to content

Commit

Permalink
Shell: change server from gunicorn to waitress (#3001)
Browse files Browse the repository at this point in the history
  • Loading branch information
lostella authored Sep 12, 2023
1 parent 83e260e commit bb7c7c1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 41 deletions.
1 change: 1 addition & 0 deletions .github/workflows/style_type_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
pip install .
pip install click black mypy
pip install types-python-dateutil
pip install types-waitress
- name: Style and type checks
run: |
just black
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-extras-shell.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
flask~=2.0
gunicorn~=19.9
waitress~=2.1.2
6 changes: 4 additions & 2 deletions src/gluonts/shell/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Optional

import click
import waitress

from gluonts.env import env as gluonts_env
from gluonts.shell.serve import Settings
Expand Down Expand Up @@ -82,12 +83,13 @@ def serve_command(
else:
forecaster_type = None

gunicorn_app = serve.make_gunicorn_app(
flask_app = serve.make_flask_app(
env=env,
forecaster_type=forecaster_type,
settings=Settings(),
)
gunicorn_app.run()

waitress.serve(flask_app, listen="*:8080")


@cli.command(name="train")
Expand Down
37 changes: 3 additions & 34 deletions src/gluonts/shell/serve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from typing import List, Optional, Type, Union

from flask import Flask
from gunicorn.app.base import BaseApplication
from pydantic import BaseSettings

import gluonts
Expand Down Expand Up @@ -94,32 +93,11 @@ def number_of_workers(self) -> int:
return cpu_count


class Application(BaseApplication):
def __init__(self, app, config) -> None:
self.app = app
self.config = config
BaseApplication.__init__(self)

def load_config(self) -> None:
for key, value in self.config.items():
if key in self.cfg.settings and value is not None:
self.cfg.set(key, value)

def init(self, parser, opts, args):
pass

def load(self) -> Flask:
return self.app

def stop(self, *args, **kwargs):
logger.info("Shutting down GluonTS scoring service")


def make_gunicorn_app(
def make_flask_app(
env: ServeEnv,
forecaster_type: Optional[Type[Union[Estimator, Predictor]]],
settings: Settings,
) -> Application:
) -> Flask:
if forecaster_type is not None:
logger.info("Using dynamic predictor factory")

Expand Down Expand Up @@ -159,13 +137,4 @@ def predictor_factory(request) -> Predictor:
settings=settings,
)

gunicorn_app = Application(
app=flask_app,
config={
"bind": settings.sagemaker_server_bind,
"workers": settings.number_of_workers,
"timeout": settings.sagemaker_server_timeout,
},
)

return gunicorn_app
return flask_app
9 changes: 6 additions & 3 deletions src/gluonts/testutil/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tempfile
import time
import typing
import waitress
from contextlib import closing, contextmanager
from dataclasses import dataclass
from multiprocessing.context import ForkContext
Expand All @@ -33,7 +34,7 @@
from gluonts.shell.env import ServeEnv, TrainEnv
from gluonts.shell.sagemaker import ServePaths, TrainPaths
from gluonts.shell.sagemaker.params import encode_sagemaker_parameters
from gluonts.shell.serve import Settings, make_gunicorn_app
from gluonts.shell.serve import Settings, make_flask_app


class ServerFacade:
Expand Down Expand Up @@ -121,10 +122,12 @@ class Server:
settings: Settings = Settings()

def run(self):
gunicorn_app = make_gunicorn_app(
flask_app = make_flask_app(
self.env, self.forecaster_type, self.settings
)
gunicorn_app.run()
waitress.serve(
flask_app, listen=f"*:{self.settings.sagemaker_server_port}"
)


@contextmanager
Expand Down
2 changes: 1 addition & 1 deletion test/shell/require-packages.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flask
gunicorn
waitress
requests

0 comments on commit bb7c7c1

Please sign in to comment.