Skip to content

Commit

Permalink
Move function creation outside of loop in example DAGs
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-fell committed Dec 5, 2023
1 parent 1712a51 commit ff1eb7a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
13 changes: 6 additions & 7 deletions airflow/example_dags/example_python_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ def log_sql(**kwargs):

# [START howto_operator_python_kwargs]
# Generate 5 sleeping tasks, sleeping from 0.0 to 0.4 seconds respectively
for i in range(5):

@task(task_id=f"sleep_for_{i}")
def my_sleeping_function(random_base):
"""This is a function that will run within the DAG execution"""
time.sleep(random_base)
@task
def my_sleeping_function(random_base):
"""This is a function that will run within the DAG execution"""
time.sleep(random_base)

sleeping_task = my_sleeping_function(random_base=i / 10)
for i in range(5):
sleeping_task = my_sleeping_function.override(task_id=f"sleep_for_{i}")(random_base=i / 10)

run_this >> log_the_sql >> sleeping_task
# [END howto_operator_python_kwargs]
Expand Down
9 changes: 4 additions & 5 deletions airflow/example_dags/example_python_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ def log_sql(**kwargs):

# [START howto_operator_python_kwargs]
# Generate 5 sleeping tasks, sleeping from 0.0 to 0.4 seconds respectively
for i in range(5):

def my_sleeping_function(random_base):
"""This is a function that will run within the DAG execution"""
time.sleep(random_base)
def my_sleeping_function(random_base):
"""This is a function that will run within the DAG execution"""
time.sleep(random_base)

for i in range(5):
sleeping_task = PythonOperator(
task_id=f"sleep_for_{i}", python_callable=my_sleeping_function, op_kwargs={"random_base": i / 10}
)
Expand Down

0 comments on commit ff1eb7a

Please sign in to comment.