mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
Adding throw argument to limit_concurrent.
This commit is contained in:
parent
3c9d9ea402
commit
034b290712
1 changed files with 6 additions and 4 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue