A flexible async/sync execution library with dedicated thread pools
Project description
Async Decorator
A flexible Python library for managing async/sync function execution with dedicated thread pools.
Usage scenarios
- Ensure that the main process is not blocked
- Provide concurrency capability for certain functions
- At the same time, some functions cannot use concurrency
- Use EXCLUSIVE type isolation for non concurrent functionality
- Use SHARED_POOL type to ensure concurrency requirements for other features
- Multi functional use of one thread to ensure serial processing of data. Tip: Transformer Large Model Reasoning
Features
- Dedicated Thread Pools: Isolate synchronous function execution
- Shared Thread Pools: Efficiently execute async functions in threads
- Flexible Execution Types: EXCLUSIVE, SHARED_POOL
- Thread Safety: Managed thread pool lifecycle
- Easy Integration: Simple decorator-based API
Installation
pip install async-decorator
Quick Start
import asyncio
import aiohttp
from async_decorator import async_decorator, ExecType
@async_decorator(ExecType.EXCLUSIVE)
def process_data(data):
# CPU-intensive synchronous work
return len(data)
@async_decorator(ExecType.SHARED_POOL)
async def fetch_data(url):
# I/O-bound async work
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def main():
# These run in appropriate thread pools
result1 = process_data("hello world")
result2 = await fetch_data("https://example.com")
print(f"Processed: {result1}, Fetched: {len(result2)} bytes")
asyncio.run(main())
Execution Types
EXCLUSIVE: Sync functions in dedicated thread poolsSHARED_POOL: Async functions in shared thread pool
Advanced Usage
Example 1
from async_decorator import ExecType, async_decorator
# Custom maximum workers for shared async pool
shared_pool_size = 200
@async_decorator(ExecType.SHARED_POOL, shared_pool_size=shared_pool_size)
def custom_processing():
return "processed"
Example 2
from async_decorator import DedicatedThreadManager, ExecType, async_decorator
# Custom thread manager, but shared_pool_size will lose its effect
manager = DedicatedThreadManager(
shared_pool_size=20,
max_dedicated_pools=50
)
@async_decorator(ExecType.EXCLUSIVE, "my_exclusive", thread_manager=manager)
def custom_processing():
return "processed"
# Cleanup
manager.shutdown()
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
async_decorator-0.1.1.tar.gz
(10.4 kB
view details)
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_decorator-0.1.1.tar.gz.
File metadata
- Download URL: async_decorator-0.1.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21addf75f5b0e1dbe6271ef4f491514f3668af257a64550287b91c9f13ee63a2
|
|
| MD5 |
f263f47e8b2f0edb8d46bd595f01716a
|
|
| BLAKE2b-256 |
9463ee7e98a598104e6ebde11c215bb256ec0c4b5050c0166788c46b8a7940c9
|
File details
Details for the file async_decorator-0.1.1-py3-none-any.whl.
File metadata
- Download URL: async_decorator-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6be371c2069308f166445c82d861a2e83ec6b38a0f780fe6964738395e9dc13d
|
|
| MD5 |
6755dd92ecdce93f9dc9312c376447ac
|
|
| BLAKE2b-256 |
129f2a73fd95f7d396985914936d007d4c435f2207d3c83bb8ec9c8d4c2966d2
|