diff --git a/docs/api.rst b/docs/api.rst index 90bbb13a..eb36108e 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -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:: @@ -1269,8 +1269,8 @@ Result Traceback (most recent call last): File "", line 1, in 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 @@ -1303,6 +1303,8 @@ 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 @@ -1310,7 +1312,7 @@ Result 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 @@ -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 diff --git a/huey/api.py b/huey/api.py index 06e17132..209f02cf 100644 --- a/huey/api.py +++ b/huey/api.py @@ -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 @@ -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: diff --git a/huey/exceptions.py b/huey/exceptions.py index 8f25dfe5..de98d99f 100644 --- a/huey/exceptions.py +++ b/huey/exceptions.py @@ -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): diff --git a/huey/tests/test_api.py b/huey/tests/test_api.py index 4ea8b044..bb651048 100644 --- a/huey/tests/test_api.py +++ b/huey/tests/test_api.py @@ -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 @@ -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):