diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-11-04 19:02:27 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-11-04 19:02:27 -0400 |
commit | 3e40d9c7b75dec008beb6b4ecf71b60e8bae088d (patch) | |
tree | c6961bc743fc1a1ee909973e216313a45febcdb2 /libdimension-python | |
parent | bdcce150b4154f84ef24b52b44801c3e735b3eb0 (diff) | |
download | dimension-3e40d9c7b75dec008beb6b4ecf71b60e8bae088d.tar.xz |
Let Future objects be used as context managers.
Diffstat (limited to 'libdimension-python')
-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.") |