async-sync - Elegant conversion between Python sync and async code
Project description
async-sync
async-sync —— 优雅地在 Python 同步和异步代码之间转换
特性
- ✨ 简单易用的 API
- 🔄 在同步代码中无缝调用异步函数
- 🔄 在异步代码中无缝调用同步函数
- 🧠 智能处理嵌套事件循环
- 🛡️ 类型提示完备
- 📦 无外部依赖
安装
pip install async-sync
使用示例
将异步函数转换为同步函数
import async_sync
import asyncio
@async_sync.to_sync
async def synced_func():
await asyncio.sleep(1)
return "Hello from async world!"
# 可以在同步代码中直接调用
print(synced_func()) # 输出:Hello from async world!
# 也可以在异步代码中调用
async def main():
print(synced_func()) # 仍然正常工作
asyncio.run(main())
将同步函数转换为异步函数
import async_sync
import time
def blocking_func(name):
time.sleep(1) # 模拟耗时操作
return f"Hello, {name}!"
# 将同步函数转换为异步函数
async_hello = async_sync.to_async(blocking_func)
# 在异步代码中调用
import asyncio
async def main():
# 可以用 await 调用
result = await async_hello("World")
print(result) # 输出:Hello, World!
# 可以并发调用多个
results = await asyncio.gather(
async_hello("Alice"),
async_hello("Bob"),
async_hello("Charlie")
)
print(results) # 输出:['Hello, Alice!', 'Hello, Bob!', 'Hello, Charlie!']
asyncio.run(main())
直接运行协程或同步函数
import async_sync
import asyncio
import time
# 在同步代码中运行协程
async def fetch_data():
await asyncio.sleep(1)
return "Data fetched"
data = async_sync.run_async_in_sync(fetch_data())
print(data) # 输出:Data fetched
# 在异步代码中运行同步函数
async def main():
def cpu_intensive_task():
time.sleep(1) # 模拟CPU密集型任务
return "Task completed"
result = await async_sync.run_sync_in_async(cpu_intensive_task)
print(result) # 输出:Task completed
asyncio.run(main())
开发
安装依赖
uv sync
运行测试
uv run pytest
commit
pre-commit install
cz commit
发布
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.0.tar.gz.
File metadata
- Download URL: async_sync-0.1.0.tar.gz
- Upload date:
- Size: 17.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 |
132efdd97b3ea28d1c32443be0e5442e272a2d8ea1e003b2a4c703b964a00308
|
|
| MD5 |
3c20352943a7142c9cf103e56c4a21c2
|
|
| BLAKE2b-256 |
7eb9a274cdf34bc92c0e0fab532da30ee8fe293586b92bd99c6801d68c55dff7
|
Provenance
The following attestation bundles were made for async_sync-0.1.0.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.0.tar.gz -
Subject digest:
132efdd97b3ea28d1c32443be0e5442e272a2d8ea1e003b2a4c703b964a00308 - Sigstore transparency entry: 194293717
- Sigstore integration time:
-
Permalink:
Haskely/async-sync@7c47d597eefdcb7f5ff5ea670975b2ee496c58c3 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Haskely
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@7c47d597eefdcb7f5ff5ea670975b2ee496c58c3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file async_sync-0.1.0-py3-none-any.whl.
File metadata
- Download URL: async_sync-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 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 |
9f555481862c22c8edc2cfc4046e5b4392e230ff8de9d9af5aee35132b1e20fa
|
|
| MD5 |
245e8f9bf99c45a6561c865402211c3e
|
|
| BLAKE2b-256 |
696b9c117a39842ced2d999f0157a8aa2aae5056c584a85d941a8d1917d5e8c0
|
Provenance
The following attestation bundles were made for async_sync-0.1.0-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.0-py3-none-any.whl -
Subject digest:
9f555481862c22c8edc2cfc4046e5b4392e230ff8de9d9af5aee35132b1e20fa - Sigstore transparency entry: 194293719
- Sigstore integration time:
-
Permalink:
Haskely/async-sync@7c47d597eefdcb7f5ff5ea670975b2ee496c58c3 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Haskely
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@7c47d597eefdcb7f5ff5ea670975b2ee496c58c3 -
Trigger Event:
push
-
Statement type: