Reputation: 85
I used to create tasks with the python operator and retrieve execution in airflow 1 as follow
def task(**kwargs):
date = kwargs['execution_date']
What is the correct way to do it with the new taskflow api ? (probably missed it)
Thanks
Upvotes: 7
Views: 8128
Reputation: 4853
You can access the execution context with get_current_context
method:
from airflow.decorators import task
from airflow.operators.python import get_current_context
@task
def my_task():
context = get_current_context()
ti = context["ti"]
date = context["execution_date"]
Docs here. Try it out!
The code above works just fine but, the so called context
objects, are directly accesible in task-decorated functions. This means that there is no need to import get_current_context
anymore. The context objects are accesible just by declaring the parameterss in the task signature:
@task
def my_task(ds=None, ti=None):
print(f"execution_date:{ds}")
print(f"task_instance:{ti}")
Upvotes: 11