From 304e3aa645623ec69d06deea64ff3c2c3eb697b5 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Thu, 30 Jun 2016 11:28:40 +0100 Subject: [PATCH] 1. The async.wait function can either be run directly, or can be run on a new thread. 2. ImageWrapper marks its expand coverage task thread as a daemon. --- fsl/data/imagewrapper.py | 1 + fsl/utils/async.py | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/fsl/data/imagewrapper.py b/fsl/data/imagewrapper.py index 6358a1b67..2651e144e 100644 --- a/fsl/data/imagewrapper.py +++ b/fsl/data/imagewrapper.py @@ -180,6 +180,7 @@ class ImageWrapper(notifier.Notifier): if threaded: self.__taskThread = async.TaskThread() + self.__taskThread.daemon = True self.__taskThread.start() diff --git a/fsl/utils/async.py b/fsl/utils/async.py index f70494549..9c4f0aab4 100644 --- a/fsl/utils/async.py +++ b/fsl/utils/async.py @@ -318,17 +318,31 @@ def wait(threads, task, *args, **kwargs): instances to finsih (by ``join``ing them), and then runs the given ``task`` via :func:`idle`. - If a ``wx.App`` is not running, this function ``join``s the threads - directly instead of creating a new ``Thread`` to do so. + If the ``direct`` parameter is ``True``, or a ``wx.App`` is not running, + this function ``join``s the threads directly instead of creating a new + ``Thread`` to do so. - :arg threads: A ``Thread``, or a sequence of ``Thread`` instances to - join. Elements in the sequence may be ``None``. + :arg threads: A ``Thread``, or a sequence of ``Thread`` instances to + join. Elements in the sequence may be ``None``. - :arg task: The task to run. + :arg task: The task to run once all ``threads`` have completed. + + :arg wait_direct: Must be passed as a keyword argument. If ``True``, this + function call will ``join`` all of the ``threads``, and + then call the ``task``. Otherwise (the default), this + function will create a new thread to ``join`` the + ``threads``, and will return immediately. + All other arguments are passed to the ``task`` function. + + + .. note:: This function will not support ``task`` functions which expect + a keyword argument called ``wait_direct``. """ + direct = kwargs.pop('wait_direct', False) + if not isinstance(threads, collections.Sequence): threads = [threads] @@ -343,7 +357,7 @@ def wait(threads, task, *args, **kwargs): log.debug('Wait thread scheduling task on idle loop') idle(task, *args, **kwargs) - if haveWX: + if haveWX and not direct: thread = threading.Thread(target=joinAll) thread.start() return thread -- GitLab