diff options
Diffstat (limited to 'libdimension-python/wrapper.pyx')
-rw-r--r-- | libdimension-python/wrapper.pyx | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/libdimension-python/wrapper.pyx b/libdimension-python/wrapper.pyx index 1afb0ea..5f2b755 100644 --- a/libdimension-python/wrapper.pyx +++ b/libdimension-python/wrapper.pyx @@ -91,6 +91,14 @@ cdef class Future: with nogil: dmnsn_future_wait(self._future, progress) + # Let Futures be used as context managers + def __enter__(self): + return self + def __exit__(self, exc_type, exc_value, traceback): + if self._future != NULL: + self.join() + return False + def _assert_unfinished(self): if self._future == NULL: raise RuntimeError("background task finished.") |