SmartAsync Documentation
Sync or async: this WAS the question
Bidirectional sync/async bridge for Python
SmartAsync provides automatic context detection to seamlessly bridge synchronous and asynchronous Python code. Write your code once, use it everywhere.
Getting Started
How It Works
Usage Scenarios
- Usage Scenarios
- Scenario A1: Sync Application Calling Async Libraries
- Overview
- Without SmartAsync
- With SmartAsync
- Issues Impact on This Scenario
- Decision Matrix
- When to Use SmartAsync for This Scenario
- Real-World Example: Data Processing Pipeline
- Migration Path
- Anti-Patterns
- Testing Strategy
- Performance Characteristics
- Checklist for This Scenario
- Summary
- Related Scenarios
- Further Reading
- Scenario A2: Async App → Sync Legacy Library
- Scenario 03: Testing Async Code
- Scenario 04: Unified Library API
- Scenario 05: Gradual Migration
- Scenario 06: Plugin Systems
- Scenario 07: Mixed Framework Integration
- Scenario 08: Web Scraping
- Scenario 09: Interactive Environments
API Reference
Integrations
Key Features
Automatic Context Detection - Detects sync vs async execution context at runtime
Bidirectional - Supports both sync→async and async→sync
Zero Configuration - Just apply the
@smartasyncdecoratorThread-Safe Offloading - Sync code in async context runs in thread pool
Pure Python - No dependencies beyond standard library
Python 3.10+ - Uses modern pattern matching
Quick Example
from smartasync import smartasync
import httpx
@smartasync
async def fetch_data(url: str):
async with httpx.AsyncClient() as client:
return await client.get(url).json()
# Sync context - no await needed!
data = fetch_data("https://api.example.com")
# Async context - use await
data = await fetch_data("https://api.example.com")
Installation
pip install smartasync
How It Works
SmartAsync uses pattern matching to handle four execution scenarios:
Part of Genro-Libs
SmartAsync is part of the Genro-Libs toolkit - a collection of focused, well-tested Python developer tools.
License
MIT License - see LICENSE for details.