|
3 | 3 | # This source code is licensed under the MIT license found in the |
4 | 4 | # LICENSE file in the root directory of this source tree. |
5 | 5 |
|
| 6 | +import sys |
| 7 | +from concurrent.futures import Executor, Future |
6 | 8 | from types import TracebackType |
7 | | -from typing import Callable, Generator, Iterable, Optional, Type, TypeVar |
| 9 | +from typing import Callable, Optional, Type, TypeVar |
8 | 10 |
|
9 | | -RetT = TypeVar("RetT") |
10 | | -ArgT = TypeVar("ArgT") |
| 11 | +if sys.version_info >= (3, 10): |
| 12 | + from typing import ParamSpec |
| 13 | +else: |
| 14 | + from typing_extensions import ParamSpec |
11 | 15 |
|
| 16 | +Return = TypeVar("Return") |
| 17 | +Params = ParamSpec("Params") |
12 | 18 |
|
13 | | -class DummyPool: |
| 19 | + |
| 20 | +class DummyExecutor(Executor): |
14 | 21 | """ |
15 | | - Synchronous dummy `multiprocessing.Pool` analogue. |
| 22 | + Synchronous dummy `concurrent.futures.Executor` analogue. |
16 | 23 | """ |
17 | 24 |
|
18 | | - def __init__(self, processes: Optional[int] = None) -> None: |
| 25 | + def __init__(self, max_workers: Optional[int] = None) -> None: |
19 | 26 | pass |
20 | 27 |
|
21 | | - def imap_unordered( |
| 28 | + def submit( |
22 | 29 | self, |
23 | | - func: Callable[[ArgT], RetT], |
24 | | - iterable: Iterable[ArgT], |
25 | | - chunksize: Optional[int] = None, |
26 | | - ) -> Generator[RetT, None, None]: |
27 | | - for args in iterable: |
28 | | - yield func(args) |
29 | | - |
30 | | - def __enter__(self) -> "DummyPool": |
| 30 | + fn: Callable[Params, Return], |
| 31 | + /, |
| 32 | + *args: Params.args, |
| 33 | + **kwargs: Params.kwargs, |
| 34 | + ) -> Future[Return]: |
| 35 | + future: Future[Return] = Future() |
| 36 | + try: |
| 37 | + result = fn(*args, **kwargs) |
| 38 | + future.set_result(result) |
| 39 | + except Exception as exc: |
| 40 | + future.set_exception(exc) |
| 41 | + return future |
| 42 | + |
| 43 | + def __enter__(self) -> "DummyExecutor": |
31 | 44 | return self |
32 | 45 |
|
33 | 46 | def __exit__( |
34 | 47 | self, |
35 | | - exc_type: Optional[Type[Exception]], |
36 | | - exc: Optional[Exception], |
37 | | - tb: Optional[TracebackType], |
| 48 | + exc_type: Optional[Type[BaseException]], |
| 49 | + exc_val: Optional[BaseException], |
| 50 | + exc_tb: Optional[TracebackType], |
38 | 51 | ) -> None: |
39 | 52 | pass |
0 commit comments