From e08ed4818a84cab6ce6122842f939e3298f94601 Mon Sep 17 00:00:00 2001 From: luav Date: Mon, 10 Jul 2017 15:29:59 +0200 Subject: [PATCH] Execution pool resuse refined --- README.md | 2 ++ mpepool.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 22544a7..9a72816 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,8 @@ ExecPool(wksnum=max(cpu_count()-1, 1), afnmask=None, memlimit=0., latency=0., na Internal attributes: alive - whether the execution pool is alive or terminating, bool. Should be reseted to True on resuse after the termination. + NOTE: should be reseted to True if the execution pool is reused + after the joining or termination. """ execute(job, async=True): diff --git a/mpepool.py b/mpepool.py index 435d7a8..9cbc8a0 100755 --- a/mpepool.py +++ b/mpepool.py @@ -785,6 +785,8 @@ def __init__(self, wksnum=max(_CPUS-1, 1), afnmask=None, memlimit=0., latency=0. Internal attributes: alive - whether the execution pool is alive or terminating, bool. Should be reseted to True on resuse after the termination. + NOTE: should be reseted to True if the execution pool is reused + after the joining or termination. """ assert (wksnum >= 1 and (afnmask is None or isinstance(afnmask, AffinityMask)) and memlimit >= 0 and latency >= 0 and (name is None or isinstance(name, str)) @@ -834,7 +836,17 @@ def __init__(self, wksnum=max(_CPUS-1, 1), afnmask=None, memlimit=0., latency=0. def __enter__(self): """Context entrence""" - self.alive = True + # Reuse execpool if possible + if not self.alive: + if not self._workers and not self._jobs: + print('WARNING{}, the non-cleared execution pool is reused' + .format('' if not self.name else ' ' + self.name)) + self._tstart = None + self.alive = True + else: + raise ValueError('Terminating dirty execution pool can not be reentered:' + ' alive: {}, {} workers, {} jobs'.format(self.alive + , len(self._workers), len(self._jobs))) return self