Async Decorator
一个灵活的 Python 库,用于通过专用线程池管理异步/同步函数执行。
使用场景
- 确保主进程不被阻塞
- 为特定函数提供并发能力
- 同时,保障某些无法使用并发的功能
- 对非并发功能使用 EXCLUSIVE 类型进行隔离
- 使用 SHARED_POOL 类型保证其他功能的并发需求
- 多功能共用一个线程,确保数据串行处理。例如:Transformer 大模型推理上的使用
功能特点
- 专用线程池:隔离同步函数执行
- 共享线程池:在线程中高效执行异步函数
- 灵活的ExecType:EXCLUSIVE、SHARED_POOL
- 线程安全:管理线程池生命周期
- 易于集成:基于装饰器的简单 API
安装
pip install async-decorator
快速开始
import asyncio
import aiohttp
from async_decorator import async_decorator, ExecType
@async_decorator(ExecType.EXCLUSIVE)
def process_data(data):
# CPU 密集型同步工作
return len(data)
@async_decorator(ExecType.SHARED_POOL)
async def fetch_data(url):
# I/O 密集型异步工作
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def main():
# 这些函数在适当的线程池中运行
result1 = process_data("hello world")
result2 = await fetch_data("https://example.com")
print(f"处理结果: {result1}, 获取数据: {len(result2)} 字节")
asyncio.run(main())
执行类型
EXCLUSIVE:在专用线程池中运行同步函数SHARED_POOL:在共享线程池中运行异步函数
高级用法
示例 1
from async_decorator import ExecType, async_decorator
# 自定义共享异步池的最大工作线程数
shared_pool_size = 200
@async_decorator(ExecType.SHARED_POOL, shared_pool_size=shared_pool_size)
def custom_processing():
return "已处理"
示例 2
from async_decorator import DedicatedThreadManager, ExecType, async_decorator
# 自定义线程管理器,但 shared_pool_size 将失效
manager = DedicatedThreadManager(
shared_pool_size=20,
max_dedicated_pools=50
)
@async_decorator(ExecType.EXCLUSIVE, "my_exclusive", thread_manager=manager)
def custom_processing():
return "已处理"
# 清理
manager.shutdown()
Metadata
Release files for async-decorator 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| async_decorator-0.1.2.tar.gz | 12.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| async_decorator-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.8 kB
Release files / async_decorator-0.1.2.tar.gz
| Download URL | async_decorator-0.1.2.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d91213ab6bd92eb3e5e7bd2c5e274919359bdf97e9e75ee8356e0098201de4ca
|
|
BLAKE2b-256 checksum How to use checksums |
453032171d669c11c36ec09f2f5d675f84c81a67fd0d74f75d20734b814396e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Release files / async_decorator-0.1.2-py3-none-any.whl
| Download URL | async_decorator-0.1.2-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a323beccd5e9f4efb6839f25c7d31d4752e25993480f9989a425fa50eb67fa2
|
|
BLAKE2b-256 checksum How to use checksums |
32ed7d9c6931849884f70c6ab16e10fc20ed0ef41997c94e5745f9cec3f1f6d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Security for async-decorator 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.