potiuk commented on code in PR #25161: URL: https://github.com/apache/airflow/pull/25161#discussion_r927902796
########## docs/apache-airflow/howto/dynamic-dag-generation.rst: ########## @@ -140,3 +140,120 @@ Each of them can run separately with related configuration .. warning:: Using this practice, pay attention to "late binding" behaviour in Python loops. See `that GitHub discussion <https://github.com/apache/airflow/discussions/21278#discussioncomment-2103559>`_ for more details + + +Optimizing DAG parsing delays during execution +---------------------------------------------- + +Sometimes when you generate a lot of Dynamic DAGs from a single DAG file, it might cause unnecessary delays +when the DAG file is parsed during task execution. The impact is a delay before a task starts. + +Why is this happening? You might not be aware but just before your task is executed, +Airflow parses the Python file the DAG comes from. + +The Airflow Scheduler (or DAG Processor) requires loading of a complete DAG file to process all metadata. +However, task execution requires only a single DAG object to execute a task. Knowing this, we can +skip the generation of unnecessary DAG objects when a task is executed, shortening the parsing time. +This optimization is most effective when the number of generated DAGs is high. + +There is an experimental approach that you can take to optimize this behaviour. Note that it is not always +possible to use (for example when generation of subsequent DAGs depends on the previous DAGs) or when +there are some side-effects of your DAGs generation. Also the code snippet below is pretty complex and while +we tested it and it works in most circumstances, there might be cases where detection of the currently +parsed DAG will fail and it will revert to creating all the DAGs or fail. Use this solution with care and +test it thoroughly. + +A nice example of performance improvements you can gain is shown in the +`Airflow's Magic Loop <https://medium.com/apache-airflow/airflows-magic-loop-ec424b05b629>`_ blog post +that describes how parsing during task execution was reduced from 120 seconds to 200 ms. + +Airflow 2.4+ +............ + +In Airflow 2.4 the following variables you can use +``airflow.utils.dag_parsing_context import get_parsing_context`` method to retrieve the current context. + +Upon iterating over the collection of things to generate DAGs for, you can use the context to determine +whether you need to generate all DAG objects (when parsing in the DAG File processor), or to generate only +a single DAG object (when executing the task): + +.. code-block:: python + :emphasize-lines: 6,7,8,12,13 + + import os + from airflow.models.dag import DAG + from airflow.utils.dag_parsing_context import get_parsing_context, AirflowParsingContextType + + current_dag = None + parsing_context = get_parsing_context() + if parsing_context and parsing_context.context_type == AirflowParsingContextType.TASK_EXECUTION: + current_dag = parsing_context.dag_id Review Comment: Indeed :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
