Skip to content

Gunicorn server

GunicornApplication

Bases: BaseApplication

Use gunicorn to wrap the flask web server.

Source code in agentuniverse/agent_serve/web/gunicorn_server.py
Python
@singleton
class GunicornApplication(BaseApplication):
    """Use gunicorn to wrap the flask web server."""
    def __init__(self, config_path: str = None):
        self.options = {}
        if config_path:
            self.__load_config_from_file(config_path)
        else:
            self.default_config = None
        self.application = app
        super().__init__()

    def load_config(self):
        """Check the config file first, use default config while config file
        not exist, then overwrite parts which in options."""
        if not self.default_config:
            config = DEFAULT_GUNICORN_CONFIG
        else:
            config = self.default_config
        for key, value in config.items():
            if key in self.cfg.settings and value is not None:
                self.cfg.set(key.lower(), value)

        # The priority of the passed arguments supersedes that of config file.
        for key, value in self.options.items():
            if key in self.cfg.settings and value is not None:
                self.cfg.set(key.lower(), value)

    def update_config(self, options: dict):
        self.options = options
        self.load_config()

    def load(self):
        return self.application

    def __load_config_from_file(self, config_path: str):
        """Load gunicorn config file."""
        try:
            with open(config_path, 'rb') as f:
                config = tomli.load(f)["GUNICORN_CONFIG"]
        except (FileNotFoundError, TypeError):
            print("can't find gunicorn config file, use default config")
            return
        except (tomli.TOMLDecodeError, KeyError):
            print("gunicorn config file isn't a valid toml, "
                  "use default config.")
            return

        self.default_config = {
            key: value for key, value in config.items()
        }

__load_config_from_file(config_path)

Load gunicorn config file.

Source code in agentuniverse/agent_serve/web/gunicorn_server.py
Python
def __load_config_from_file(self, config_path: str):
    """Load gunicorn config file."""
    try:
        with open(config_path, 'rb') as f:
            config = tomli.load(f)["GUNICORN_CONFIG"]
    except (FileNotFoundError, TypeError):
        print("can't find gunicorn config file, use default config")
        return
    except (tomli.TOMLDecodeError, KeyError):
        print("gunicorn config file isn't a valid toml, "
              "use default config.")
        return

    self.default_config = {
        key: value for key, value in config.items()
    }

load_config()

Check the config file first, use default config while config file not exist, then overwrite parts which in options.

Source code in agentuniverse/agent_serve/web/gunicorn_server.py
Python
def load_config(self):
    """Check the config file first, use default config while config file
    not exist, then overwrite parts which in options."""
    if not self.default_config:
        config = DEFAULT_GUNICORN_CONFIG
    else:
        config = self.default_config
    for key, value in config.items():
        if key in self.cfg.settings and value is not None:
            self.cfg.set(key.lower(), value)

    # The priority of the passed arguments supersedes that of config file.
    for key, value in self.options.items():
        if key in self.cfg.settings and value is not None:
            self.cfg.set(key.lower(), value)