You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/08/12 13:58:39 UTC

[GitHub] [airflow] yuqian90 commented on a change in pull request #10153: [AIP-34] Alternative proposal: Add TaskGroup as a DAG construction helper and UI concept with example

yuqian90 commented on a change in pull request #10153:
URL: https://github.com/apache/airflow/pull/10153#discussion_r469280080



##########
File path: airflow/utils/task_group.py
##########
@@ -0,0 +1,233 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+TaskGroup
+"""
+
+from typing import List, Optional
+
+
+class TaskGroup:
+    """
+    A collection of tasks. Tasks within the TaskGroup have their task_id prefixed with the
+    group_id of the TaskGroup. When set_downstream() or set_upstream() are called on the
+    TaskGroup, it is applied across all tasks within the group if necessary.
+    """
+    def __init__(self, group_id, parent_group=None):
+        if group_id is None:
+            # This creates a root TaskGroup.
+            self.parent_group = None
+        else:
+            if not isinstance(group_id, str):
+                raise ValueError("group_id must be str")
+            if not group_id:
+                raise ValueError("group_id must not be empty")
+            self.parent_group = parent_group or TaskGroupContext.get_current_task_group()
+
+        self._group_id = group_id
+        if self.parent_group:
+            self.parent_group.add(self)
+        self.children = {}
+
+    @classmethod
+    def create_root(cls):
+        """
+        Create a root TaskGroup with no group_id or parent.
+        """
+        return cls(group_id=None)
+
+    @property
+    def is_root(self):
+        """
+        Returns True if this TaskGroup is the root TaskGroup. Otherwise False
+        """
+        return not self.parent_group
+
+    def __iter__(self):
+        for child in self.children.values():
+            if isinstance(child, TaskGroup):
+                for inner_task in child:
+                    yield inner_task
+            else:
+                yield child
+
+    def add(self, task):
+        """
+        Add a task to this TaskGroup.
+        """
+        if task.label in self.children:
+            raise ValueError(f"Duplicate label {task.label} in {self.group_id}")
+        self.children[task.label] = task
+
+    @property
+    def label(self):
+        """
+        group_id excluding parent's group_id.
+        """
+        return self._group_id
+
+    @property
+    def group_id(self):
+        """
+        group_id is prefixed with parent group_id if applicable.
+        """
+        if not self.group_ids:
+            return None
+
+        return ".".join(self.group_ids)
+
+    @property
+    def group_ids(self):
+        """
+        Returns all the group_id of nested TaskGroups as a list, starting from the top.
+        """
+        return list(self._group_ids())[1:]
+
+    def _group_ids(self):
+        if self.parent_group:
+            for group_id in self.parent_group._group_ids():  # pylint: disable=protected-access
+                yield group_id
+
+        yield self._group_id
+
+    def set_downstream(self, task_or_task_list) -> None:

Review comment:
       Sure. will do.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org