Simple, reliable async/sync conversion for Python. Call async from sync and vice versa.
Project description
asyncbridge
Call async functions from sync code. Call sync functions from async code. No drama.
The Problem
You have an async function but you're in sync code:
async def fetch_user(user_id: int) -> User:
...
# This doesn't work in sync code!
user = fetch_user(123) # Returns coroutine, not User
Or you have sync code but need to call it from async without blocking:
def expensive_computation(data: bytes) -> Result:
# CPU-intensive, blocks the event loop
...
The Solution
from asyncbridge import async_to_sync, sync_to_async
# Async → Sync
fetch_user_sync = async_to_sync(fetch_user)
user = fetch_user_sync(123) # Just works!
# Sync → Async (runs in thread pool)
compute_async = sync_to_async(expensive_computation)
result = await compute_async(data) # Doesn't block event loop
Installation
pip install asyncbridge
Features
- Zero dependencies - only stdlib
- Type-safe - full type hints, works with mypy
- Thread-safe - proper event loop handling
- Decorator support - use as decorator or wrapper
API
async_to_sync(func) → sync function
Converts an async function to sync. Handles event loop creation/reuse correctly.
from asyncbridge import async_to_sync
@async_to_sync
async def my_async_func():
await asyncio.sleep(1)
return "done"
result = my_async_func() # Blocks until complete
sync_to_async(func) → async function
Runs sync function in thread pool executor to avoid blocking event loop.
from asyncbridge import sync_to_async
@sync_to_async
def blocking_io():
time.sleep(1)
return "done"
result = await blocking_io() # Runs in thread, doesn't block loop
When to Use
| Situation | Solution |
|---|---|
| Calling async library from sync script | async_to_sync |
| Using async ORM in sync framework | async_to_sync |
| CPU-bound work in async server | sync_to_async |
| Blocking I/O in async code | sync_to_async |
Alternatives Comparison
| Package | Deps | Framework-agnostic | Maintained |
|---|---|---|---|
| asyncbridge | 0 | ✅ | ✅ |
| asgiref | 1 | Django-focused | ✅ |
| nest-asyncio | 0 | Different purpose | ✅ |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncbridge-1.0.0.tar.gz.
File metadata
- Download URL: asyncbridge-1.0.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a49d56093a6f28919ecea9a515c61da2d0c829e4df680bbef07284537b5db03
|
|
| MD5 |
db4becbd9b0d5fca9ac530f006906614
|
|
| BLAKE2b-256 |
ec828594aa70f45e63a7eff863edb08027143941c045ed3affcc194df7c9a935
|
File details
Details for the file asyncbridge-1.0.0-py3-none-any.whl.
File metadata
- Download URL: asyncbridge-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed0402aaacb1526033e130ba4ce512b1256711aed88ee6444e67eb95b5e24a88
|
|
| MD5 |
9fc3820ad3d390ff3cdfd0bfad5f671f
|
|
| BLAKE2b-256 |
c9c431ba1b6bf7e24af15c8f9b46863378b98828f8e29fb806d23c39a4a25984
|