From 54879a1f60bf720b141a73fa73aef0fa6b276c88 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Fri, 11 Oct 2019 14:54:49 +0100 Subject: [PATCH] ENH: idle.block now accepts a conditional function which can control how long it blocks for --- fsl/utils/idle.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/fsl/utils/idle.py b/fsl/utils/idle.py index c1184a31a..7c4c88106 100644 --- a/fsl/utils/idle.py +++ b/fsl/utils/idle.py @@ -391,27 +391,41 @@ def cancelIdle(taskName): _idleQueueDict[taskName].timeout = -1 -def block(secs, delta=0.01): +def block(secs, delta=0.01, until=None): """Blocks for the specified number of seconds, yielding to the main ``wx`` loop. If ``wx`` is not available, or a ``wx`` application is not running, this function is equivalent to ``time.sleep(secs)``. + If ``until`` is provided, this function will block until ``until`` + returns ``True``, or ``secs`` have elapsed, whichever comes first. + :arg secs: Time in seconds to block :arg delta: Time in seconds to sleep between successive yields to ``wx``. + :arg until: Function which returns ``True`` or ``False``, and which + determins when calls to ``block`` will return. """ - from fsl.utils.platform import platform as fslplatform + def defaultUntil(): + return False - if not fslplatform.haveGui: - time.sleep(secs) - else: - import wx - start = time.time() - while (time.time() - start) < secs: + def tick(): + if fslplatform.haveGui: + import wx wx.YieldIfNeeded() - time.sleep(delta) + time.sleep(delta) + + if until is None: + until = defaultUntil + + from fsl.utils.platform import platform as fslplatform + + start = time.time() + while (time.time() - start) < secs: + tick() + if until(): + break def idle(task, *args, **kwargs): -- GitLab