Skip to content

Commit 6b8a08b

Browse files
authoredNov 6, 2019
Merge pull request #207 from weka-io/add-done-to-concurrent
Add `done()` function to `concurrent` to check if the thread is finished
2 parents 20c7834 + 70e9bf3 commit 6b8a08b

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
### Added
1414
- units: Added a Percentage class
1515
- timing: Added a TimeInterval class, for use for timestamp comparisons. Can be converted to/from the Timer class
16+
- concurrency: Add `done()` function to `concurrent` to check if the thread is finished.
1617

1718

1819
## [0.3.1] - 2019-07-30

‎easypy/concurrency.py

+6
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,12 @@ def result(self, timeout=None):
10211021
raise self.exc
10221022
return self._result
10231023

1024+
def done(self):
1025+
"""
1026+
Return ``True`` if the thread is done (successfully or not)
1027+
"""
1028+
return hasattr(self, '_result') or getattr(self, 'exc', None) is not None
1029+
10241030
@contextmanager
10251031
def paused(self):
10261032
self.stop()

‎tests/test_concurrency.py

+19
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,22 @@ def raise_it(typ):
322322

323323
with pytest.raises(MultiException[OK]):
324324
MultiObject([OKBAD]).call(raise_it)
325+
326+
327+
@pytest.mark.parametrize('throw', [False, True])
328+
def test_concurrent_done_status(throw):
329+
from threading import Event
330+
331+
continue_func = Event()
332+
333+
def func():
334+
continue_func.wait()
335+
if throw:
336+
raise Exception()
337+
338+
with concurrent(func, throw=False) as c:
339+
assert not c.done()
340+
continue_func.set()
341+
sleep(0.1)
342+
assert c.done()
343+
assert c.done()

0 commit comments

Comments
 (0)
Please sign in to comment.