async-sync - Elegant conversion between Python sync and async code
Project description
async-sync
async-sync — Elegantly convert between Python synchronous and asynchronous code
Features
- ✨ Simple and easy-to-use API
- 🔄 Seamlessly call async functions in synchronous code
- 🔄 Seamlessly call synchronous functions in async code
- 🧠 Intelligent handling of nested event loops
- 🛡️ Comprehensive type hints
- 📦 No external dependencies
Installation
pip install async-sync
Usage Examples
Convert async functions to sync functions
import async_sync
import asyncio
@async_sync.to_sync
async def synced_func():
await asyncio.sleep(1)
return "Hello from async world!"
# Can be called directly in synchronous code
print(synced_func()) # Output: Hello from async world!
# Also works in async code
async def main():
print(synced_func()) # Still works normally
asyncio.run(main())
Convert sync functions to async functions
import async_sync
import time
def blocking_func(name):
time.sleep(1) # Simulate time-consuming operation
return f"Hello, {name}!"
# Convert synchronous function to asynchronous
async_hello = async_sync.to_async(blocking_func)
# Use in async code
import asyncio
async def main():
# Can be called with await
result = await async_hello("World")
print(result) # Output: Hello, World!
# Can be called concurrently
results = await asyncio.gather(
async_hello("Alice"),
async_hello("Bob"),
async_hello("Charlie")
)
print(results) # Output: ['Hello, Alice!', 'Hello, Bob!', 'Hello, Charlie!']
asyncio.run(main())
Directly run coroutines or sync functions
import async_sync
import asyncio
import time
# Run coroutines in synchronous code
async def fetch_data():
await asyncio.sleep(1)
return "Data fetched"
data = async_sync.run_async_in_sync(fetch_data())
print(data) # Output: Data fetched
# Run synchronous functions in async code
async def main():
def cpu_intensive_task():
time.sleep(1) # Simulate CPU-intensive task
return "Task completed"
result = await async_sync.run_sync_in_async(cpu_intensive_task)
print(result) # Output: Task completed
asyncio.run(main())
Development
Install dependencies
uv sync
Run tests
uv run pytest
Commit
pre-commit install
cz commit
Release
cz bump
git push --follow-tags
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 async_sync-0.1.1.tar.gz.
File metadata
- Download URL: async_sync-0.1.1.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
746fa84b413b38a6697aef69d20a65a0c395e7500addbb28433f8458fc449b08
|
|
| MD5 |
b66feabf8243d4cb6e34ed87a4a4f7c7
|
|
| BLAKE2b-256 |
6108b01b51690b52d621358106a41f3307164ed00e86675a968f9bff81dab356
|
Provenance
The following attestation bundles were made for async_sync-0.1.1.tar.gz:
Publisher:
publish-to-pypi.yml on Haskely/async-sync
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_sync-0.1.1.tar.gz -
Subject digest:
746fa84b413b38a6697aef69d20a65a0c395e7500addbb28433f8458fc449b08 - Sigstore transparency entry: 194295891
- Sigstore integration time:
-
Permalink:
Haskely/async-sync@9b8b7572737c19324b348939310662a27340c26f -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/Haskely
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@9b8b7572737c19324b348939310662a27340c26f -
Trigger Event:
push
-
Statement type:
File details
Details for the file async_sync-0.1.1-py3-none-any.whl.
File metadata
- Download URL: async_sync-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a5bc1460743291f83e146dc2679a1ed9d049cbdbd6d37132d64aa81b129c458
|
|
| MD5 |
a7d912bc981081625f5596b38e818852
|
|
| BLAKE2b-256 |
de81fe14f3c68d0285b48035b8e2698b28b33c045a8bc5867d186039dcc6fb0a
|
Provenance
The following attestation bundles were made for async_sync-0.1.1-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on Haskely/async-sync
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_sync-0.1.1-py3-none-any.whl -
Subject digest:
2a5bc1460743291f83e146dc2679a1ed9d049cbdbd6d37132d64aa81b129c458 - Sigstore transparency entry: 194295894
- Sigstore integration time:
-
Permalink:
Haskely/async-sync@9b8b7572737c19324b348939310662a27340c26f -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/Haskely
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@9b8b7572737c19324b348939310662a27340c26f -
Trigger Event:
push
-
Statement type: