Skip to content
Snippets Groups Projects
Commit 54879a1f authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

ENH: idle.block now accepts a conditional function which can control how long

it blocks for
parent cab37727
No related branches found
No related tags found
No related merge requests found
...@@ -391,27 +391,41 @@ def cancelIdle(taskName): ...@@ -391,27 +391,41 @@ def cancelIdle(taskName):
_idleQueueDict[taskName].timeout = -1 _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`` """Blocks for the specified number of seconds, yielding to the main ``wx``
loop. loop.
If ``wx`` is not available, or a ``wx`` application is not running, this If ``wx`` is not available, or a ``wx`` application is not running, this
function is equivalent to ``time.sleep(secs)``. 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 secs: Time in seconds to block
:arg delta: Time in seconds to sleep between successive yields to ``wx``. :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: def tick():
time.sleep(secs) if fslplatform.haveGui:
else: import wx
import wx
start = time.time()
while (time.time() - start) < secs:
wx.YieldIfNeeded() 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): def idle(task, *args, **kwargs):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment