Esempio n. 1
0
class _WorkItem(object):
    def __init__(self, future, fn, args, kwargs):
        self.future = future
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.loop = evergreen.current.loop
        # Keep the loop alive while this work item is queued
        self.handler = InfiniteHandler(self.loop)
        self._event = threading.Event()
        self._result = None
        self._exc = None
        self._cancelled = False

    def run(self):
        self.loop.call_from_thread(self._set_running)
        self._event.wait()
        if self._cancelled:
            return
        try:
            r = self.fn(*self.args, **self.kwargs)
            self.loop.call_from_thread(self.future.set_result, r)
        except BaseException as e:
            self.loop.call_from_thread(self.future.set_exception, e)
        finally:
            self.loop.call_from_thread(self.handler.cancel)
            self.loop = self.handler = None

    def _set_running(self):
        if not self.future.set_running_or_notify_cancel():
            self.handler.cancel()
            self._cancelled = True
        self._event.set()
Esempio n. 2
0
 def __init__(self, future, fn, args, kwargs):
     self.future = future
     self.fn = fn
     self.args = args
     self.kwargs = kwargs
     self.loop = evergreen.current.loop
     # Keep the loop alive while this work item is queued
     self.handler = InfiniteHandler(self.loop)
     self._event = threading.Event()
     self._cancelled = False
Esempio n. 3
0
 def __init__(self, future, fn, args, kwargs):
     self.future = future
     self.fn = fn
     self.args = args
     self.kwargs = kwargs
     self.loop = evergreen.current.loop
     # Keep the loop alive while this work item is queued
     self.handler = InfiniteHandler(self.loop)
     self._event = threading.Event()
     self._cancelled = False
Esempio n. 4
0
class _WorkItem(object):
    def __init__(self, future, fn, args, kwargs):
        self.future = future
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.loop = evergreen.current.loop
        # Keep the loop alive while this work item is queued
        self.handler = InfiniteHandler(self.loop)
        self._event = threading.Event()
        self._cancelled = False

    def is_cancelled(self):
        self.loop.call_from_thread(self._set_running)
        self._event.wait()
        return self._cancelled

    def _set_running(self):
        if not self.future.set_running_or_notify_cancel():
            self.handler.cancel()
            self._cancelled = True
        self._event.set()
Esempio n. 5
0
class _WorkItem(object):
    def __init__(self, future, fn, args, kwargs):
        self.future = future
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.loop = evergreen.current.loop
        # Keep the loop alive while this work item is queued
        self.handler = InfiniteHandler(self.loop)
        self._event = threading.Event()
        self._cancelled = False

    def is_cancelled(self):
        self.loop.call_from_thread(self._set_running)
        self._event.wait()
        return self._cancelled

    def _set_running(self):
        if not self.future.set_running_or_notify_cancel():
            self.handler.cancel()
            self._cancelled = True
        self._event.set()