Refactor async call limiting to use asyncio.Semaphore for better performance.

- Replace custom counter with asyncio.Semaphore
- The existing implementation cannot follow the FIFO order
This commit is contained in:
yangdx
2025-02-01 10:36:15 +08:00
parent 635d4fd9e4
commit b109f57ddd

View File

@@ -124,21 +124,16 @@ def compute_mdhash_id(content, prefix: str = ""):
return prefix + md5(content.encode()).hexdigest() return prefix + md5(content.encode()).hexdigest()
def limit_async_func_call(max_size: int, waitting_time: float = 0.0001): def limit_async_func_call(max_size: int):
"""Add restriction of maximum async calling times for a async func""" """Add restriction of maximum concurrent async calls using asyncio.Semaphore"""
def final_decro(func): def final_decro(func):
"""Not using async.Semaphore to aovid use nest-asyncio""" sem = asyncio.Semaphore(max_size)
__current_size = 0
@wraps(func) @wraps(func)
async def wait_func(*args, **kwargs): async def wait_func(*args, **kwargs):
nonlocal __current_size async with sem:
while __current_size >= max_size:
await asyncio.sleep(waitting_time)
__current_size += 1
result = await func(*args, **kwargs) result = await func(*args, **kwargs)
__current_size -= 1
return result return result
return wait_func return wait_func