diff --git a/airflow/cli/commands/webserver_command.py b/airflow/cli/commands/webserver_command.py index 9d15d44b7dfcd..ce45705ee0064 100644 --- a/airflow/cli/commands/webserver_command.py +++ b/airflow/cli/commands/webserver_command.py @@ -414,6 +414,12 @@ def webserver(args): run_args += ["airflow.www.app:cached_app()"] + # To prevent different workers creating the web app and + # all writing to the database at the same time, we use the --preload option. + # With the preload option, the app is loaded before the workers are forked, and each worker will + # then have a copy of the app + run_args += ['--preload'] + gunicorn_master_proc = None def kill_proc(signum, _): diff --git a/tests/cli/commands/test_webserver_command.py b/tests/cli/commands/test_webserver_command.py index 873661323406f..b81b7f6e33b60 100644 --- a/tests/cli/commands/test_webserver_command.py +++ b/tests/cli/commands/test_webserver_command.py @@ -405,6 +405,7 @@ def test_cli_webserver_args(self): "--access-logformat", "custom_log_format", "airflow.www.app:cached_app()", + "--preload", ], close_fds=True, )