jaketf commented on a change in pull request #6210: [AIRFLOW-5567] [Do not Merge] prototype BaseAsyncOperator URL: https://github.com/apache/airflow/pull/6210#discussion_r331808485
########## File path: airflow/models/base_async_operator.py ########## @@ -0,0 +1,166 @@ +# -*- coding: utf-8 -*- +# +# 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. + +""" +Base Asynchronous Operator for kicking off a long running +operations and polling for completion with reschedule mode. +""" + +from abc import abstractmethod +from typing import Dict, List, Union, Optional + +from airflow.sensors.base_sensor_operator import BaseSensorOperator +from airflow.exceptions import AirflowException +from airflow.models.xcom import XCOM_EXTERNAL_RESOURCE_ID_KEY +from airflow.models import SkipMixin, TaskReschedule +from airflow.utils.decorators import apply_defaults + +PLACEHOLDER_RESOURCE_ID = 'RESOURCE_ID_NOT_APPLICABLE' + + +class BaseAsyncOperator(BaseSensorOperator, SkipMixin): + """ + AsyncOperators are derived from this class and inherit these attributes. + AsyncOperators should be used for long running operations where the task + can tolerate a longer poke interval. They use the task rescheduling + mechanism similar to sensors to avoid occupying a worker slot between + pokes. + + Developing concrete operators that provide parameterized flexibility + for synchronous or asynchronous poking depending on the invocation is + possible by programing against this `BaseAsyncOperator` interface, + and overriding the execute method as demonstrated below. + + ```python3 + class DummyFlexiblePokingOperator(BaseAsyncOperator): + def __init__(self, async=False, *args, **kwargs): + self.async = async + super().__init(*args, **kwargs) + + def execute(self, context: Dict) -> None: + if self.async: + # use the BaseAsyncOperator's execute + super().execute(context) + else: + self.submit_request(context) + while not self.poke(): + time.sleep(self.poke_interval) + self.process_results(context) + + def sumbit_request(self, context: Dict) -> Optional[str]: + return None + def poke(self, context: Dict) -> bool: + return bool(random.getrandbits(1)) + ``` Review comment: @nuclearpinguin Does this update to doc string help clear up the confusion on how to do operators that could be sync or async? ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
