From 034b290712a6906bbb592c75b0d40d1bd47cfa61 Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 27 Oct 2022 17:41:38 -0600 Subject: [PATCH] Adding throw argument to limit_concurrent. --- wrolpi/common.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/wrolpi/common.py b/wrolpi/common.py index 1594ef40..04ae0b07 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -946,10 +946,8 @@ def print_timer(): f' {calls} calls', file=sys.stderr) -def limit_concurrent(limit: int): - """ - Wrapper that limits the amount of concurrently running functions. - """ +def limit_concurrent(limit: int, throw: bool = False): + """Wrapper that limits the amount of concurrently running functions.""" sema = multiprocessing.Semaphore(value=limit) def wrapper(func: callable): @@ -958,6 +956,8 @@ def limit_concurrent(limit: int): async def wrapped(*a, **kw): acquired = sema.acquire(block=False) if not acquired: + if throw: + raise ValueError(f'Reached concurrent limit! {func=}') return try: return await func(*a, **kw) @@ -970,6 +970,8 @@ def limit_concurrent(limit: int): def wrapped(*a, **kw): acquired = sema.acquire(block=False) if not acquired: + if throw: + raise ValueError(f'Reached concurrent limit! {func=}') return try: return func(*a, **kw)