Files
chia-blockchain/chia/util/inline_executor.py
T
Arvid NorbergandGitHub 1bcf409ee1 single thread executor (#10919)
* add inline executor and an option to run single-threaded

* add option to run test_full_sync in single-thread mode, to include block validation in profiles. Also attempt to speed it up by disabling db_sync
2022-03-28 12:47:46 -07:00

25 lines
646 B
Python

from __future__ import annotations
from concurrent.futures import Executor, Future
from typing import Callable, TypeVar
_T = TypeVar("_T")
class InlineExecutor(Executor):
_closing: bool = False
def submit(self, fn: Callable[..., _T], *args, **kwargs) -> Future[_T]: # type: ignore
if self._closing:
raise RuntimeError("executor shutting down")
f: Future[_T] = Future()
try:
f.set_result(fn(*args, **kwargs))
except BaseException as e: # lgtm[py/catch-base-exception]
f.set_exception(e)
return f
def close(self) -> None:
self._closing = True