Skip to content
Snippets Groups Projects
Commit 911e7134 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

New async function idleWhen, which allows a condition function to

dictate when the actual function is called.
parent e7095d0d
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ Idle tasks ...@@ -20,6 +20,7 @@ Idle tasks
:nosignatures: :nosignatures:
idle idle
idleWhen
inIdle inIdle
...@@ -323,6 +324,29 @@ def idle(task, *args, **kwargs): ...@@ -323,6 +324,29 @@ def idle(task, *args, **kwargs):
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): def wait(threads, task, *args, **kwargs):
"""Creates and starts a new ``Thread`` which waits for all of the ``Thread`` """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 instances to finsih (by ``join``ing them), and then runs the given
......
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