From 911e7134ff05e34902b71911eb877a6cf7de4f0b Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Wed, 27 Jul 2016 11:05:24 +0100
Subject: [PATCH] New async function idleWhen, which allows a condition
 function to dictate when the actual function is called.

---
 fsl/utils/async.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/fsl/utils/async.py b/fsl/utils/async.py
index 28c255041..67e0bf65e 100644
--- a/fsl/utils/async.py
+++ b/fsl/utils/async.py
@@ -20,6 +20,7 @@ Idle tasks
    :nosignatures:
 
    idle
+   idleWhen
    inIdle
 
 
@@ -323,6 +324,29 @@ def idle(task, *args, **kwargs):
         task(*args, **kwargs)
 
 
+def idleWhen(func, condition, *args, **kwargs):
+    """Poll the ``condition`` function periodically, and schedule ``func`` on
+    :func:`idle` when it returns ``True``.
+
+    :arg func:      Function to call.
+    
+    :arg condition: Function which returns ``True`` or ``False``. The ``func``
+                    function is only called when the ``condition`` function 
+                    returns ``True``.
+    
+    :arg pollTime:  Must be passed as a keyword argument. Time (in seconds) to
+                    wait between successive calls to ``when``.
+    """
+
+    pollTime = kwargs.get('pollTime', 0.2)
+
+    if not condition():
+        idle(idleWhen, func, condition, after=pollTime, *args, **kwargs)
+    else:
+        kwargs.pop('pollTime', None)
+        idle(func, *args, **kwargs)
+
+
 def wait(threads, task, *args, **kwargs):
     """Creates and starts a new ``Thread`` which waits for all of the ``Thread``
     instances to finsih (by ``join``ing them), and then runs the given
-- 
GitLab