Skip to content

Commit

Permalink
Add new ResultTimeout subclass for when Result.get times out.
Browse files Browse the repository at this point in the history
Fixes #810
  • Loading branch information
coleifer committed Aug 13, 2024
1 parent 0dfc0a3 commit 895f635
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
15 changes: 11 additions & 4 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ Huey object
yet. If you want to wait for the result, specify ``blocking=True``.
This will loop, backing off up to the provided ``max_delay``, until the
value is ready or the ``timeout`` is reached. If the ``timeout`` is
reached before the result is ready, a :py:class:`HueyException` will be
reached before the result is ready, a :py:class:`ResultTimeout` will be
raised.

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

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

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

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

.. py:class:: ResultTimeout
Raised when attempting to block on a call to :py:meth:`Result.get` (for
instance) and the timeout is exceeded without the result being ready.

.. py:class:: CancelExecution
Cancel the execution of a task. Can be raised either within a
Expand Down
3 changes: 2 additions & 1 deletion huey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from huey.exceptions import CancelExecution
from huey.exceptions import ConfigurationError
from huey.exceptions import HueyException
from huey.exceptions import ResultTimeout
from huey.exceptions import RetryTask
from huey.exceptions import TaskException
from huey.exceptions import TaskLockedException
Expand Down Expand Up @@ -998,7 +999,7 @@ def get_raw_result(self, blocking=False, timeout=None, backoff=1.15,
if timeout and time_clock() - start >= timeout:
if revoke_on_timeout:
self.revoke()
raise HueyException('timed out waiting for result')
raise ResultTimeout('timed out waiting for result')
if delay > max_delay:
delay = max_delay
if self._get(preserve) is EmptyData:
Expand Down
1 change: 1 addition & 0 deletions huey/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class HueyException(Exception): pass
class ConfigurationError(HueyException): pass
class TaskLockedException(HueyException): pass
class ResultTimeout(HueyException): pass

class CancelExecution(Exception):
def __init__(self, retry=None, *args, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions huey/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from huey.constants import EmptyData
from huey.exceptions import CancelExecution
from huey.exceptions import ConfigurationError
from huey.exceptions import ResultTimeout
from huey.exceptions import RetryTask
from huey.exceptions import TaskException
from huey.exceptions import TaskLockedException
Expand Down Expand Up @@ -88,6 +89,18 @@ def task_a(n):
self.assertEqual(self.huey.result_count(), 1)
self.assertTrue(r4._get() is None)

def test_result_timeout(self):
@self.huey.task()
def task_a(n):
return n

r = task_a(1)
with self.assertRaises(ResultTimeout):
r.get(blocking=True, timeout=0.01)
self.assertEqual(self.execute_next(), 1)
self.assertEqual(self.huey.result_count(), 1)
self.assertEqual(r(), 1)

def test_scheduling(self):
@self.huey.task()
def task_a(n):
Expand Down

0 comments on commit 895f635

Please sign in to comment.