Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app_factory): speed up api startup #11762

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
from app_factory import create_app
from libs import threadings_utils, version_utils
from libs import version_utils

# preparation before creating app
version_utils.check_supported_python_version()
threadings_utils.apply_gevent_threading_patch()


def is_db_command():
import sys

if len(sys.argv) > 1 and sys.argv[0].endswith("flask") and sys.argv[1] == "db":
return True
return False


# create app
app = create_app()
celery = app.extensions["celery"]
if is_db_command():
from app_factory import create_migrations_app

app = create_migrations_app()
else:
from app_factory import create_app
from libs import threadings_utils

threadings_utils.apply_gevent_threading_patch()

app = create_app()
celery = app.extensions["celery"]

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001)
11 changes: 11 additions & 0 deletions api/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,14 @@ def initialize_extensions(app: DifyApp):
end_time = time.perf_counter()
if dify_config.DEBUG:
logging.info(f"Loaded {short_name} ({round((end_time - start_time) * 1000, 2)} ms)")


def create_migrations_app():
app = create_flask_app_with_configs()
from extensions import ext_database, ext_migrate

# Initialize only required extensions
ext_database.init_app(app)
ext_migrate.init_app(app)

return app
Loading