villebro commented on code in PR #36368:
URL: https://github.com/apache/superset/pull/36368#discussion_r2673009730


##########
docs/developer_portal/async-tasks.md:
##########
@@ -0,0 +1,460 @@
+---
+title: Async Task Framework
+sidebar_label: Async Tasks
+sidebar_position: 5
+---
+
+
+<!--
+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.
+-->
+
+# Global Async Task Framework (GATF)
+
+The Global Async Task Framework provides a unified way to manage asynchronous 
tasks in Apache Superset. It handles task registration, execution, status 
tracking, cancellation, and deduplication.
+
+## Overview
+
+GATF uses the **ambient context pattern** where tasks access their execution 
context via `get_context()` instead of receiving it as a parameter. This 
results in clean, business-focused function signatures without framework 
boilerplate.
+
+### Key Features
+
+- **Clean Signatures**: Task functions contain only business args
+- **Ambient Context**: Access context via `get_context()` - no parameter 
passing
+- **Dual Execution**: Synchronous (for testing) and asynchronous (via Celery)
+- **Optional Deduplication**: Use idempotency keys to prevent duplicate 
execution
+- **Progressive Updates**: Update payload and check cancellation during 
execution
+- **Type Safety**: Full type hints with ParamSpec support
+
+## Quick Start
+
+### Define a Task
+
+```python
+import requests
+from superset_core.api.types import async_task, get_context
+
+@async_task()
+def fetch_data(api_url: str) -> None:
+    """
+    Example task that fetches data from an external API.
+
+    Features:
+    - Automatic cancellation check before execution
+    - Simple cleanup handler
+    - Cancellation checking during execution
+    """
+    ctx = get_context()
+
+    # Cleanup runs automatically on success, failure, or cancellation
+    @ctx.on_cleanup
+    def cleanup():
+        logger.info("Data fetch completed")
+
+    # No initial check needed - framework checks before execution!
+    # Fetch data with timeout (prevents hanging)
+    response = requests.get(api_url, timeout=60)
+    data = response.json()
+
+    # Check before next operation
+    if ctx.is_cancelled():
+        return
+
+    # Process and cache the data
+    process_and_cache(data)
+```
+
+### Execute Tasks Asynchronously or Synchronously
+
+The `@async_task` decorator enables flexible execution modes:
+
+```python
+# Asynchronous execution via Celery (for production workloads)
+task = long_running_task.schedule()
+# Task runs in background worker, returns immediately
+print(task.status)  # "pending"
+
+# Synchronous execution (for testing or when blocking is acceptable)
+task = long_running_task()
+# Task executes inline, blocks until complete
+print(task.status)  # "success"
+```
+
+**When to use each mode:**

Review Comment:
   I'm not sure I understand. We can definitely have async tests where 
necessary. But providing the possibility to trigger the task sync and async 
using the same code path will make it easier to write unit tests that verify 
the business logic without having to spawn the full async environment. Since 
the host is responsible for making sure that `schedule()` works as expected, 
it's not necessary for individual tasks to verify that that's indeed the case 
(the async executor will have tests of its own to verify that the plumbing 
works as expected).



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to