You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "raphaelauv (via GitHub)" <gi...@apache.org> on 2025/05/07 09:43:28 UTC
[D] MappedTaskGroup without taskflow [airflow]
GitHub user raphaelauv created a discussion: MappedTaskGroup without taskflow
Hi I'm trying to use dynamic task mapping on a taskgroup without the taskflow syntax
this is working ->
```python
from datetime import timedelta
import time
from airflow import DAG
from airflow.providers.standard.operators.python import PythonOperator
from airflow.decorators import task_group
from pendulum import today
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
) as dag:
def sleep(duration):
time.sleep(duration)
@task_group(group_id="group_1", dag=dag)
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
tg1.expand(duration=[1, 10, 20, 30])
```
but I would like to not mix traditional syntax with taskflow syntax, and I did not yet found a way using
```
from airflow.utils.task_group import MappedTaskGroup
```
do you have any idea if it's possible ? thanks all
GitHub link: https://github.com/apache/airflow/discussions/50297
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv edited a comment on the discussion: MappedTaskGroup without taskflow
"solution" until a MappedTaskGroup is available in the task-sdk
```python
from airflow.sdk import DAG
from airflow.providers.standard.operators.python import PythonOperator
from typing import Callable
import time
def mapped_taskgroup_builder(tg_callable: Callable, group_id: str, **tg_kwargs):
from airflow.sdk import task_group
tg = task_group(group_id=group_id, **tg_kwargs)
return tg(tg_callable)
with DAG(
dag_id="group_dag",
schedule=None,
):
first = PythonOperator(
task_id="first",
python_callable=lambda: [1, 10, 20, 30]
)
def sleep(duration):
time.sleep(duration)
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
mapped_taskgroup_builder(group_id="g1", tg_callable=tg1, tooltip="SUPER").expand(duration=first.output)
```
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-17510964
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
I see. As for scheduling strategy mapped task group (depth-first) for sure takes approach different from mapped tasks (breadth-first).
I personally would want to have depth-first strategy by default, especially when there’s significant difference in workload between the mapped tasks but seems that they haven’t make it a general available option for now, while the decorated task group is a makeshift solution (or to say, exception) for now.
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13263242
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
From documentation it's not recommended to use `MappedTaskGroup` directly ([reference](https://github.com/apache/airflow/blob/47e4fe6926fda9ffbd595a0985f05c22e272a61f/task-sdk/src/airflow/sdk/definitions/taskgroup.py#L612-L613)) so I don't think it's not a good idea.
If you want to avoid decorator syntax in DAG definition, it may be better to wire task inputs/outputs just at task level instead of using a mapped task group. Looks a bit redundant but I think it is logically equivalent to the original one. What about giving this adjustment a try?
```python
...
from airflow.utils.task_group import TaskGroup
...
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
):
first = PythonOperator(
task_id="first",
python_callable=lambda : [1, 10, 20, 30]
)
def sleep(duration):
# It's recommended to place module imports in the task function (subroutine),
# so that all required imports only happens when the task gets run.
import time
time.sleep(duration)
with TaskGroup(group_id="group_1"):
a = PythonOperator.partial(task_id="a", python_callable=sleep).expand(duration=duration.output)
b = PythonOperator.partial(task_id="b", python_callable=sleep).expand(duration=duration.output)
c = PythonOperator.partial(task_id="a", python_callable=sleep).expand(duration=duration.output)
a >> b >> c
```
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13262214
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
I see. As for scheduling strategy mapped task group (depth-first) for sure takes approach different from not using task group (breadth-first).
I personally would want to have depth-first strategy by default, especially when there’s significant difference in workload between the mapped tasks) but seems that they haven’t make it a general available option for now, while the task group is a makeshift solution (or to say, exception) for now.
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13263242
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny added a comment to the discussion: MappedTaskGroup without taskflow
I see. As for scheduling strategy mapped task group (depth-first) applies different approach from not using task group (breadth-first).
I personally would want to have depth-first strategy by default, especially when there’s significant difference in workload between the mapped tasks) but seems that they haven’t make it a general available option for now, while the task group exception is a makeshift solution for now.
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13263242
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
I see. As for scheduling strategy mapped task group (depth-first) for sure takes approach different from not using task group (breadth-first).
I personally would want to have depth-first strategy by default, especially when there’s significant difference in workload between the mapped tasks) but seems that they haven’t make it a general available option for now, while the task group exception is a makeshift solution for now.
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13263242
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv edited a comment on the discussion: MappedTaskGroup without taskflow
"solution" until a MappedTaskGroup is available in the task-sdk
```python
from airflow.sdk import DAG
from airflow.providers.standard.operators.python import PythonOperator
from typing import Callable
import time
def mapped_taskgroup_builder(group_id: str, tg_callable: Callable):
from airflow.sdk import task_group
tg = task_group(group_id=group_id)
return tg(tg_callable)
with DAG(
dag_id="group_dag",
schedule=None,
):
first = PythonOperator(
task_id="first",
python_callable=lambda: [1, 10, 20, 30]
)
def sleep(duration):
time.sleep(duration)
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
mapped_taskgroup_builder(group_id="g1",tg_callable=tg1).expand(duration=first.output)
```
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-17510964
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv added a comment to the discussion: MappedTaskGroup without taskflow
"solution" until a MappedTaskGroup is avaialable in the task-sdk
```python
from airflow.sdk import DAG
from airflow.providers.standard.operators.python import PythonOperator
from typing import Callable
import time
def mapped_taskgroup_builder(group_id: str, tg_callable: Callable):
from airflow.sdk import task_group
tg = task_group(group_id=group_id)
return tg(tg_callable)
with DAG(
dag_id="group_dag",
schedule=None,
):
first = PythonOperator(
task_id="first",
python_callable=lambda: [1, 10, 20, 30]
)
def sleep(duration):
time.sleep(duration)
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
mapped_taskgroup_builder(group_id="g1",tg_callable=tg1).expand(duration=first.output)
```
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-17510964
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv edited a discussion: MappedTaskGroup without taskflow
Hi I'm trying to use dynamic task mapping on a taskgroup without the taskflow syntax
It works with the taskflow syntax ->
```python
from datetime import timedelta
import time
from airflow import DAG
from airflow.providers.standard.operators.python import PythonOperator
from airflow.decorators import task_group
from pendulum import today
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
):
first = PythonOperator(
task_id="first",
python_callable=lambda : [1, 10, 20, 30]
)
def sleep(duration):
time.sleep(duration)
@task_group(group_id="group_1")
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
tg1.expand(duration=first.output)
```
but I would like to not mix traditional syntax with taskflow syntax, and I did not yet found a way using
```
from airflow.utils.task_group import MappedTaskGroup
```
do you have any idea if it's possible ? thanks all
GitHub link: https://github.com/apache/airflow/discussions/50297
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
I see. As for scheduling strategy mapped task group (depth-first) for sure takes approach different from not using task group (breadth-first).
I personally would want to have depth-first strategy by default, especially when there’s significant difference in workload between the mapped tasks) but seems that they haven’t make it a general available option for now, while the decorated task group is a makeshift solution (or to say, exception) for now.
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13263242
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
I see. As for scheduling strategy mapped task group (depth-first) for sure takes approach different from mapped tasks (breadth-first).
I personally would want to have depth-first strategy by default, especially when there’s significant difference in workload between the mapped tasks) but seems that they haven’t make it a general available option for now, while the decorated task group is a makeshift solution (or to say, exception) for now.
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13263242
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv edited a discussion: MappedTaskGroup without taskflow
Hi I'm trying to use dynamic task mapping on a taskgroup without the taskflow syntax
this is working ->
```python
from datetime import timedelta
import time
from airflow import DAG
from airflow.providers.standard.operators.python import PythonOperator
from airflow.decorators import task_group
from pendulum import today
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
) as dag:
first = PythonOperator(
task_id="first",
python_callable=lambda : [1, 10, 20, 30]
)
def sleep(duration):
time.sleep(duration)
@task_group(group_id="group_1", dag=dag)
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
tg1.expand(duration=first.output)
```
but I would like to not mix traditional syntax with taskflow syntax, and I did not yet found a way using
```
from airflow.utils.task_group import MappedTaskGroup
```
do you have any idea if it's possible ? thanks all
GitHub link: https://github.com/apache/airflow/discussions/50297
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv edited a discussion: MappedTaskGroup without taskflow
Hi I'm trying to use dynamic task mapping on a taskgroup without the taskflow syntax
this is working ->
```python
from datetime import timedelta
import time
from airflow import DAG
from airflow.providers.standard.operators.python import PythonOperator
from airflow.decorators import task_group
from pendulum import today
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
):
first = PythonOperator(
task_id="first",
python_callable=lambda : [1, 10, 20, 30]
)
def sleep(duration):
time.sleep(duration)
@task_group(group_id="group_1")
def tg1(duration):
a = PythonOperator(task_id="a", python_callable=sleep, op_kwargs={"duration": duration})
b = PythonOperator(task_id="b", python_callable=sleep, op_kwargs={"duration": duration})
c = PythonOperator(task_id="c", python_callable=sleep, op_kwargs={"duration": duration})
a >> b >> c
tg1.expand(duration=first.output)
```
but I would like to not mix traditional syntax with taskflow syntax, and I did not yet found a way using
```
from airflow.utils.task_group import MappedTaskGroup
```
do you have any idea if it's possible ? thanks all
GitHub link: https://github.com/apache/airflow/discussions/50297
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny edited a comment on the discussion: MappedTaskGroup without taskflow
From documentation it's not recommended to use `MappedTaskGroup` directly ([reference](https://github.com/apache/airflow/blob/47e4fe6926fda9ffbd595a0985f05c22e272a61f/task-sdk/src/airflow/sdk/definitions/taskgroup.py#L612-L613)) so I don't think it's not a good idea.
If you want to avoid decorator syntax in DAG definition, it may be better to wire task inputs/outputs just at task level instead of using a mapped task group. What about giving this adjustment a try? Looks a bit redundant but I think it is logically equivalent to the original one.
```python
...
from airflow.utils.task_group import TaskGroup
...
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
):
first = PythonOperator(
task_id="first",
python_callable=lambda : [1, 10, 20, 30]
)
def sleep(duration):
# It's recommended to place module imports in the task function (subroutine),
# so that all required imports only happens when the task gets run.
import time
time.sleep(duration)
with TaskGroup(group_id="group_1"):
a = PythonOperator.partial(task_id="a", python_callable=sleep).expand(duration=duration.output)
b = PythonOperator.partial(task_id="b", python_callable=sleep).expand(duration=duration.output)
c = PythonOperator.partial(task_id="a", python_callable=sleep).expand(duration=duration.output)
a >> b >> c
```
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13262214
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "pykenny (via GitHub)" <gi...@apache.org>.
GitHub user pykenny added a comment to the discussion: MappedTaskGroup without taskflow
From documentation it's not recommended to use `MappedTaskGroup` directly ([reference](https://github.com/apache/airflow/blob/47e4fe6926fda9ffbd595a0985f05c22e272a61f/task-sdk/src/airflow/sdk/definitions/taskgroup.py#L612-L613)) so I don't think it's not a good idea.
If you want to avoid decorator syntax in DAG definition, it may be better to wire task inputs/outputs just at task level instead of using a mapped task task group. What about giving this adjustment a try?
```python
...
from airflow.utils.task_group import TaskGroup
...
with DAG(
dag_id="dag_group_DTM",
start_date=today("UTC").add(days=-1),
schedule=timedelta(days=1),
):
first = PythonOperator(
task_id="first",
python_callable=lambda : [1, 10, 20, 30]
)
def sleep(duration):
# It's recommended to place module imports in the task function (subroutine),
# so that all required imports only happens when the task gets run.
import time
time.sleep(duration)
with TaskGroup(group_id="group_1"):
a = PythonOperator.partial(task_id="a", python_callable=sleep).expand(duration=duration.output)
b = PythonOperator.partial(task_id="b", python_callable=sleep).expand(duration=duration.output)
c = PythonOperator.partial(task_id="a", python_callable=sleep).expand(duration=duration.output)
a >> b >> c
```
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13262214
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org
Re: [D] MappedTaskGroup without taskflow [airflow]
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
GitHub user raphaelauv added a comment to the discussion: MappedTaskGroup without taskflow
This is not equivalent, in your example all A takes must end before start all B tasks
In my example when the A(1) task end the B(1) task start directly whiteout waiting
GitHub link: https://github.com/apache/airflow/discussions/50297#discussioncomment-13262594
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org