Pythonのfutures
パッケージを使用すると、タスクを並行して楽しむことができThreadPoolExecutor
ますProcessPoolExecutor
。
ただし、デバッグの場合、スレッドやプロセスを生成せずに、メインスレッドでタスクをシリアルに実行するダミーの並列処理に一時的に置き換えると便利な場合があります。
どこかに実装はありDummyExecutor
ますか?
Pythonのfutures
パッケージを使用すると、タスクを並行して楽しむことができThreadPoolExecutor
ますProcessPoolExecutor
。
ただし、デバッグの場合、スレッドやプロセスを生成せずに、メインスレッドでタスクをシリアルに実行するダミーの並列処理に一時的に置き換えると便利な場合があります。
どこかに実装はありDummyExecutor
ますか?
このような何かがそれを行う必要があります:
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
この場合、ロックはおそらく必要ありませんが、持っていても害はありません。