Executor Import/Load time optimization #30361
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR aims to improve the time it takes to load/import the various executor modules we have in Airflow.
Motivation
The executors are imported in more places now that various compatibility checks are in core Airflow code (re: AIP-51). Also, decreasing import times is very important for the work of executors vending CLI commands (see #29055), since the CLI code in Airflow is particularly sensitive to slow imports (because all code is loaded fresh each time you run an individual Airflow CLI command).
The changes
This PR mostly includes changes to move some expensive imports that are only used for type checking under the
TYPE_CHECKING
flag so that they are not run at runtime. As well as moving a select few expensive imports closer to the code which uses them.The most important changes are in the
BaseExecutor
module since all other executors load this module, and so benefits made here propagate outward.Testing
I benchmarked these changes by writing a script to import the various executor modules in a fresh python runtime and timing how long that takes (you can test this yourself quickly from a bash shell by doing something like
time python -c 'from airflow.executors.local_executor import LocalExecutor'
). Then doing that in a loop for several samples (with some randomness in the order for fairness) both on main and on my development branch.Results
Most executors saw a ~50% speed increase. Kubernetes, and to a lesser extent Celery, are still quite slow and will need more changes specifically targeted to those modules (in a future PR).
The combined executors (e.g. LocalKubernetesExecutor) saw less gains since they import two executors each, so they're paying double the cost (so they saw half the gains, 25%)
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rst
or{issue_number}.significant.rst
, in newsfragments.