|
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 | +ParamSpec = ParamSpec("ParamSpec") |
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 | + # pyre-ignore |
| 31 | + fn: Callable[ParamSpec, Return], |
| 32 | + # pyre-ignore |
| 33 | + *args: ParamSpec.args, |
| 34 | + # pyre-ignore |
| 35 | + **kwargs: ParamSpec.kwargs, |
| 36 | + # pyre-ignore |
| 37 | + ) -> Future[Return]: |
| 38 | + future: Future[Return] = Future() |
| 39 | + try: |
| 40 | + result = fn(*args, **kwargs) |
| 41 | + future.set_result(result) |
| 42 | + except Exception as exc: |
| 43 | + future.set_exception(exc) |
| 44 | + return future |
| 45 | + |
| 46 | + def __enter__(self) -> "DummyExecutor": |
31 | 47 | return self
|
32 | 48 |
|
33 | 49 | def __exit__(
|
34 | 50 | self,
|
35 |
| - exc_type: Optional[Type[Exception]], |
36 |
| - exc: Optional[Exception], |
37 |
| - tb: Optional[TracebackType], |
| 51 | + exc_type: Optional[Type[BaseException]], |
| 52 | + exc_val: Optional[BaseException], |
| 53 | + exc_tb: Optional[TracebackType], |
38 | 54 | ) -> None:
|
39 | 55 | pass
|
0 commit comments