From 5a5c22be31b3cdd73c0e1f508f7d386f5a5d61f7 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Wed, 23 Oct 2019 17:33:02 +0100 Subject: [PATCH] ENH: New IdleLoop.synchronous context manager which sets/restores the neverQueue flag. --- fsl/utils/idle.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/fsl/utils/idle.py b/fsl/utils/idle.py index afa6247ae..d2c93aeb0 100644 --- a/fsl/utils/idle.py +++ b/fsl/utils/idle.py @@ -79,6 +79,7 @@ import atexit import logging import functools import threading +from contextlib import contextmanager from collections import abc try: import queue @@ -225,15 +226,31 @@ class IdleLoop(object): @property def neverQueue(self): """If ``True``, tasks passed to :meth:`idle` will never be queued, and - instead will always be executed directly/synchonously. + instead will always be executed directly/synchonously. See also the + :meth:`synchronous` context manager. """ return self.__neverQueue @neverQueue.setter - def neverQueue(self, allow): + def neverQueue(self, val): """Update the ``neverQueue`` flag. """ - self.__neverQueue = allow + self.__neverQueue = val + + + @contextmanager + def synchronous(self): + """Context manager which can be used to tenporarily set :meth:`neverQueue` to + ``True``, restoring its previous value afterwards. + """ + + oldval = self.__neverQueue + self.__neverQueue = True + + try: + yield + finally: + self.__neverQueue = oldval def reset(self): -- GitLab