Skip to content

Commit a31408f

Browse files
committed
Init pool impl grouped by file path
1 parent b01e842 commit a31408f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/auditwheel/pool.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from concurrent.futures import Future, ThreadPoolExecutor
2+
from pathlib import Path
3+
from typing import Any, Callable, Optional
4+
5+
6+
class FileTaskExecutor:
7+
def __init__(self, concurrent: bool = False):
8+
self.executor = ThreadPoolExecutor() if concurrent else None
9+
self.working_map: dict[Path, Future[tuple[str, str]]] = {}
10+
11+
def submit(
12+
self, path: Path, fn: Callable[[Any], Any], /, *args: Any, **kwargs: Any
13+
) -> Future[Any]:
14+
future: Future[Any]
15+
if self.executor is None:
16+
future = Future()
17+
future.set_result(fn(*args, **kwargs))
18+
return future
19+
assert path not in self.working_map
20+
future = self.executor.submit(fn, *args, **kwargs)
21+
future.add_done_callback(lambda f: self.working_map.pop(path))
22+
self.working_map[path] = future
23+
return future
24+
25+
def wait(self, path: Optional[Path] = None) -> None:
26+
if self.executor is None:
27+
return
28+
if path is not None:
29+
if path in self.working_map:
30+
self.working_map.pop(path).result()
31+
return
32+
33+
for path in self.working_map:
34+
self.wait(path)

0 commit comments

Comments
 (0)