Add restriction of maximum async calling times for a async func
(max_size: int, waitting_time: float = 0.0001)
| 91 | |
| 92 | |
| 93 | def limit_async_func_call(max_size: int, waitting_time: float = 0.0001): |
| 94 | """Add restriction of maximum async calling times for a async func""" |
| 95 | |
| 96 | def final_decro(func): |
| 97 | """Not using async.Semaphore to aovid use nest-asyncio""" |
| 98 | __current_size = 0 |
| 99 | |
| 100 | @wraps(func) |
| 101 | async def wait_func(*args, **kwargs): |
| 102 | nonlocal __current_size |
| 103 | while __current_size >= max_size: |
| 104 | await asyncio.sleep(waitting_time) |
| 105 | __current_size += 1 |
| 106 | result = await func(*args, **kwargs) |
| 107 | __current_size -= 1 |
| 108 | return result |
| 109 | |
| 110 | return wait_func |
| 111 | |
| 112 | return final_decro |
| 113 | |
| 114 | |
| 115 | def wrap_embedding_func_with_attrs(**kwargs): |