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.

Usage Scenarios

API Reference

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 @smartasync decorator

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

Indices and tables