Add restriction of maximum async calling times for a async func
(max_size: int, waitting_time: float = 0.0001)
| 274 | |
| 275 | # Decorators ------------------------------------------------------------------------ |
| 276 | def limit_async_func_call(max_size: int, waitting_time: float = 0.0001): |
| 277 | """Add restriction of maximum async calling times for a async func""" |
| 278 | |
| 279 | def final_decro(func): |
| 280 | """Not using async.Semaphore to aovid use nest-asyncio""" |
| 281 | __current_size = 0 |
| 282 | |
| 283 | @wraps(func) |
| 284 | async def wait_func(*args, **kwargs): |
| 285 | nonlocal __current_size |
| 286 | while __current_size >= max_size: |
| 287 | await asyncio.sleep(waitting_time) |
| 288 | __current_size += 1 |
| 289 | result = await func(*args, **kwargs) |
| 290 | __current_size -= 1 |
| 291 | return result |
| 292 | |
| 293 | return wait_func |
| 294 | |
| 295 | return final_decro |
| 296 | |
| 297 | |
| 298 | def wrap_embedding_func_with_attrs(**kwargs): |