Skip to content

Commit 84c7645

Browse files
committed
Executor: implement limits
and accept "standard" arguments
1 parent f6e7218 commit 84c7645

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

trio_asyncio/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,23 @@ def _select(self, r, w, x, timeout=None): # pragma: no cover
7070
class TrioExecutor:
7171
"""An executor that runs its job in a Trio worker thread."""
7272

73-
def __init__(self, limiter=None):
73+
def __init__(self, limiter=None, thread_name_prefix=None, max_workers=None):
7474
self._running = True
75+
if limiter is None and max_workers is not None:
76+
limiter = trio.CapacityLimiter(max_workers)
7577
self._limiter = limiter
76-
# TODO: actually use the limiter
7778

7879
async def submit(self, func, *args):
7980
if not self._running: # pragma: no cover
8081
raise RuntimeError("Executor is down")
81-
return await trio.run_sync_in_worker_thread(func, *args, limiter=self._limiter)
82+
lim = self._limiter
83+
if lim is not None:
84+
await lim.acquire()
85+
try:
86+
return await trio.run_sync_in_worker_thread(func, *args, limiter=self._limiter)
87+
finally:
88+
if lim is not None:
89+
lim.release()
8290

8391
def shutdown(self, wait=None):
8492
self._running = False

0 commit comments

Comments
 (0)