`DummyExecutor` for Python Futures`

The Python futures package allows us to enjoy ThreadPoolExecutor and ProcessPoolExecutor for parallel tasks.

However, for debugging, it is sometimes useful to temporarily replace true parallelism with a dummy one, which performs tasks in a sequential order in the main thread without creating threads or processes.

Is there a DummyExecutor implementation DummyExecutor ?

+7
source share
1 answer

Something like this should do this:

 from concurrent.futures import Future, Executor from threading import Lock class DummyExecutor(Executor): def __init__(self): self._shutdown = False self._shutdownLock = Lock() def submit(self, fn, *args, **kwargs): with self._shutdownLock: if self._shutdown: raise RuntimeError('cannot schedule new futures after shutdown') f = Future() try: result = fn(*args, **kwargs) except BaseException as e: f.set_exception(e) else: f.set_result(result) return f def shutdown(self, wait=True): with self._shutdownLock: self._shutdown = True if __name__ == '__main__': def fnc(err): if err: raise Exception("test") else: return "ok" ex = DummyExecutor() print(ex.submit(fnc, True)) print(ex.submit(fnc, False)) ex.shutdown() ex.submit(fnc, True) # raises exception 

a lock is probably not needed in this case, but cannot damage it.

+5
source

All Articles