Skip to content

Commit 895f635

Browse files
committed
Add new ResultTimeout subclass for when Result.get times out.
Fixes #810
1 parent 0dfc0a3 commit 895f635

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

docs/api.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ Huey object
775775
yet. If you want to wait for the result, specify ``blocking=True``.
776776
This will loop, backing off up to the provided ``max_delay``, until the
777777
value is ready or the ``timeout`` is reached. If the ``timeout`` is
778-
reached before the result is ready, a :py:class:`HueyException` will be
778+
reached before the result is ready, a :py:class:`ResultTimeout` will be
779779
raised.
780780

781781
.. seealso::
@@ -1269,8 +1269,8 @@ Result
12691269
Traceback (most recent call last):
12701270
File "<stdin>", line 1, in <module>
12711271
File "/home/charles/tmp/huey/src/huey/huey/queue.py", line 46, in get
1272-
raise HueyException
1273-
huey.exceptions.HueyException
1272+
raise ResultTimeout
1273+
huey.exceptions.ResultTimeout
12741274

12751275
>>> res(blocking=True) # No timeout, will block until it gets data.
12761276
300
@@ -1303,14 +1303,16 @@ Result
13031303
attempting to fetch result.
13041304
:param bool revoke_on_timeout: if a timeout occurs, revoke the task,
13051305
thereby preventing it from running if it is has not started yet.
1306+
:raises: ResultTimeout if blocking and timeout specified without result
1307+
becoming ready yet.
13061308

13071309
Attempt to retrieve the return value of a task. By default,
13081310
:py:meth:`~Result.get` will simply check for the value, returning
13091311
``None`` if it is not ready yet. If you want to wait for a value, you
13101312
can specify ``blocking=True``. This will loop, backing off up to the
13111313
provided ``max_delay``, until the value is ready or the ``timeout`` is
13121314
reached. If the ``timeout`` is reached before the result is ready, a
1313-
:py:class:`HueyException` exception will be raised.
1315+
:py:class:`ResultTimeout` exception will be raised.
13141316

13151317
.. note:: Instead of calling ``.get()``, you can simply call the
13161318
:py:class:`Result` object directly. Both methods accept the same
@@ -1430,6 +1432,11 @@ Exceptions
14301432
14311433
Raised by the consumer when a task lock cannot be acquired.
14321434

1435+
.. py:class:: ResultTimeout
1436+
1437+
Raised when attempting to block on a call to :py:meth:`Result.get` (for
1438+
instance) and the timeout is exceeded without the result being ready.
1439+
14331440
.. py:class:: CancelExecution
14341441
14351442
Cancel the execution of a task. Can be raised either within a

huey/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from huey.exceptions import CancelExecution
2020
from huey.exceptions import ConfigurationError
2121
from huey.exceptions import HueyException
22+
from huey.exceptions import ResultTimeout
2223
from huey.exceptions import RetryTask
2324
from huey.exceptions import TaskException
2425
from huey.exceptions import TaskLockedException
@@ -998,7 +999,7 @@ def get_raw_result(self, blocking=False, timeout=None, backoff=1.15,
998999
if timeout and time_clock() - start >= timeout:
9991000
if revoke_on_timeout:
10001001
self.revoke()
1001-
raise HueyException('timed out waiting for result')
1002+
raise ResultTimeout('timed out waiting for result')
10021003
if delay > max_delay:
10031004
delay = max_delay
10041005
if self._get(preserve) is EmptyData:

huey/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class HueyException(Exception): pass
22
class ConfigurationError(HueyException): pass
33
class TaskLockedException(HueyException): pass
4+
class ResultTimeout(HueyException): pass
45

56
class CancelExecution(Exception):
67
def __init__(self, retry=None, *args, **kwargs):

huey/tests/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from huey.constants import EmptyData
1212
from huey.exceptions import CancelExecution
1313
from huey.exceptions import ConfigurationError
14+
from huey.exceptions import ResultTimeout
1415
from huey.exceptions import RetryTask
1516
from huey.exceptions import TaskException
1617
from huey.exceptions import TaskLockedException
@@ -88,6 +89,18 @@ def task_a(n):
8889
self.assertEqual(self.huey.result_count(), 1)
8990
self.assertTrue(r4._get() is None)
9091

92+
def test_result_timeout(self):
93+
@self.huey.task()
94+
def task_a(n):
95+
return n
96+
97+
r = task_a(1)
98+
with self.assertRaises(ResultTimeout):
99+
r.get(blocking=True, timeout=0.01)
100+
self.assertEqual(self.execute_next(), 1)
101+
self.assertEqual(self.huey.result_count(), 1)
102+
self.assertEqual(r(), 1)
103+
91104
def test_scheduling(self):
92105
@self.huey.task()
93106
def task_a(n):

0 commit comments

Comments
 (0)