-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from alipay/dev_fanen
Fix: Start a flask server on Windows instead of gunicorn server. Fix RequestOrm column type define.
- Loading branch information
Showing
8 changed files
with
117 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# !/usr/bin/env python3 | ||
# -*- coding:utf-8 -*- | ||
|
||
# @Time : 2024/4/23 18:10 | ||
# @Author : fanen.lhy | ||
# @Email : [email protected] | ||
# @FileName: gunicorn_server.py | ||
|
||
import tomli | ||
from gunicorn.app.base import BaseApplication | ||
|
||
from .flask_server import app | ||
from ...base.annotation.singleton import singleton | ||
|
||
|
||
DEFAULT_GUNICORN_CONFIG = { | ||
'bind': '127.0.0.1:8888', | ||
'workers': 5, | ||
'backlog': 2048, | ||
'worker_class': 'gthread', | ||
'threads': 4, | ||
'timeout': 60, | ||
'keepalive': 10 | ||
} | ||
|
||
|
||
@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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,79 +6,22 @@ | |
# @Email : [email protected] | ||
# @FileName: web_booster.py | ||
|
||
import tomli | ||
from gunicorn.app.base import BaseApplication | ||
|
||
from .web_server import app | ||
from agentuniverse.base.annotation.singleton import singleton | ||
|
||
|
||
DEFAULT_GUNICORN_CONFIG = { | ||
'bind': '127.0.0.1:8000', | ||
'workers': 5, | ||
'backlog': 2048, | ||
'worker_class': 'gthread', | ||
'threads': 4, | ||
'timeout': 60, | ||
'keepalive': 10 | ||
} | ||
|
||
|
||
@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() | ||
} | ||
import sys | ||
|
||
|
||
def start_web_server(**kwargs): | ||
"""Start func of flask server. Accept input arguments to overwrite default | ||
gunicorn config.""" | ||
GunicornApplication().update_config(kwargs) | ||
GunicornApplication().run() | ||
"""Start func of flask server(on windows platform) or gunicorn server(on | ||
others). Accept input arguments to overwrite default gunicorn config.""" | ||
if sys.platform.startswith('win'): | ||
from .flask_server import app | ||
if 'bind' in kwargs: | ||
host, port = kwargs['bind'].split(':') | ||
port = int(port) | ||
else: | ||
port = 8888 | ||
host = '0.0.0.0' | ||
app.run(port=port, host=host, debug=False) | ||
else: | ||
from .gunicorn_server import GunicornApplication | ||
GunicornApplication().update_config(kwargs) | ||
GunicornApplication().run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
tests/test_agentuniverse/unit/agent_serve/test_web_booster.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import os | ||
import pytest | ||
|
||
from agentuniverse.agent_serve.web import web_booster | ||
from agentuniverse.base.agentuniverse import AgentUniverse | ||
|
||
|
||
def test_service(): | ||
os.environ['OPENAI_API_KEY'] = 'you openai api key' | ||
# os.environ['OPENAI_API_KEY'] = 'you openai api key' | ||
AgentUniverse().start(config_path='../agent/config.toml') | ||
web_booster.start_web_server(bind="127.0.0.1:8002") | ||
|
||
|
||
if __name__ == "__main__": | ||
# os.environ['OPENAI_API_KEY'] = 'you openai api key' | ||
AgentUniverse().start(config_path='../agent/config.toml') | ||
web_booster.start_web_server(bind="0.0.0.0:8002") | ||
pytest.main([__file__, "-s"]) | ||
|