function async_action
A decorator function that marks another function as asynchronous by adding an 'is_async' attribute, while preserving the original function's metadata.
/tf/active/vicechatdev/CDocs/controllers/__init__.py
170 - 179
simple
Purpose
This decorator is designed to tag action functions as asynchronous for future implementation of async functionality. Currently acts as a pass-through decorator that adds metadata (is_async=True) to the wrapped function without changing its behavior. It's intended for use in a system where actions need to be identified as async for routing, scheduling, or execution purposes.
Source Code
def async_action(func):
"""
Decorator to mark action as asynchronous.
This is a placeholder for future implementation.
"""
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper.is_async = True
return wrapper
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
func |
- | - | positional_or_keyword |
Parameter Details
func: The function to be decorated. Can be any callable (function or method) that will be marked as asynchronous. No constraints on the function signature - it accepts any parameters and return type.
Return Value
Returns a wrapper function that behaves identically to the input function but has an additional attribute 'is_async' set to True. The wrapper preserves the original function's name, docstring, and other metadata via functools.wraps. The return type matches whatever the decorated function returns.
Dependencies
functools
Required Imports
from functools import wraps
Usage Example
from functools import wraps
def async_action(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper.is_async = True
return wrapper
# Example usage
@async_action
def process_document(doc_id, user_id):
"""Process a document asynchronously."""
print(f"Processing document {doc_id} for user {user_id}")
return {"status": "processed", "doc_id": doc_id}
# Check if function is marked as async
print(process_document.is_async) # Output: True
# Call the function normally
result = process_document(123, 456)
print(result) # Output: {'status': 'processed', 'doc_id': 123}
Best Practices
- This is a placeholder decorator - the actual async behavior is not implemented yet, so decorated functions will still execute synchronously
- Use this decorator to mark functions that should be executed asynchronously in future implementations
- Check for the 'is_async' attribute on functions to determine if they should be handled asynchronously: hasattr(func, 'is_async') and func.is_async
- The decorator preserves function metadata using @wraps, so introspection tools will work correctly
- Can be stacked with other decorators, but order may matter depending on what other decorators do
- Since this is a placeholder, ensure your codebase has a plan for implementing actual async execution when needed
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function log_controller_action 50.5% similar
-
function async_execute 50.3% similar
-
function transaction 49.1% similar
-
function run_analysis_async 42.7% similar
-
function require_auth 42.1% similar