Guide
Detailed documentation can be found in the API documentation.
Example task() that adds two numbers:
# demo.py
from huey import SqliteHuey
huey = SqliteHuey(filename='/tmp/demo.db')
@huey.task()
def add(a, b):
return a + b
Run the consumer, specifying the import path to the huey object:
huey_consumer demo.huey
In a Python shell, we can call our add task:
>>> from demo import add
>>> r = add(1, 2)
>>> r()
3
If you try to resolve the result (r) before the task has been executed,
then r() will return None. You can avoid this by instructing the
result to block until the task has finished and a result is ready:
>>> r = add(1, 2)
>>> r(blocking=True, timeout=5) # Wait up to 5 seconds for result.
3
You can also check if a result is ready using Result.is_ready():
>>> r = add(1, 2)
>>> r.is_ready()
False
>>> r.is_ready() # A few moments later.
True
>>> r()
3
What happens when we call a task function?
When the
add()function is called, a message representing the call is placed in a queue.The function returns immediately without actually running, and returns a
Resulthandle, which can be used to retrieve the result once the task has been executed by the consumer.The consumer process sees that a message has arrived, and a worker will call the
add()function and place the return value into the result store.We can use the
Resulthandle to read the return value from the result store.
For more information, see the task() decorator documentation.
Scheduling tasks
Tasks can be scheduled to execute at a certain time, or after a delay.
>>> r = add.schedule((3, 4), delay=10)
>>> r(blocking=True) # Will block for ~10 seconds before returning.
7
If we wished to schedule the task to run at a particular time, we can use the
eta parameter instead:
>>> eta = datetime.datetime.now() + datetime.timedelta(seconds=10)
>>> r = add.schedule((4, 5), eta=eta)
>>> r(blocking=True) # Will block for ~10 seconds.
9
What happens when we schedule a task?
When we call
schedule(), a message is placed on the queue instructing the consumer to call theadd()function in 10 seconds.The function returns immediately, and returns a
Resulthandle.The consumer process sees that a message has arrived, and will notice that the message is not yet ready to be executed, but should be run in ~10s.
The consumer adds the message to a schedule.
In ~10 seconds, the scheduler will pick-up the message and place it back into the queue for execution.
A worker will dequeue the message, execute the
add()function, and place the return value in the result store.The
Resulthandle from step 2 will now be able to read the return value from the task.
For more details, see the schedule() API documentation.
Periodic tasks
Huey provides crontab-like functionality that enables functions to be executed automatically on a given schedule.
from huey import SqliteHuey
from huey import crontab
huey = SqliteHuey(filename='/tmp/demo.db')
@huey.task()
def add(a, b):
return a + b
@huey.periodic_task(crontab(minute='*/3'))
def every_three_minutes():
print('This task runs every three minutes')
Once a minute, the scheduler will check if any of the periodic tasks should be called. If so, the task will be enqueued for execution.
Periodic tasks do not accept any arguments, and their return-value is discarded.
The crontab() function accepts the following arguments, in the same
order as the standard Linux crontab format:
minute
hour
day
month
day_of_week (0=Sunday, 6=Saturday)
Acceptable inputs:
*- always true, e.g. ifhour='*', then the rule matches any hour.*/n- every n interval, e.g.minute='*/15'means every 15 minutes.m-n- run every timem..ninclusive.m,n- run on m and n.
Multiple rules can be expressed by separating the individual rules with a comma, for example:
# Runs every 10 minutes between 9a and 11a, and 4p-6p.
crontab(minute='*/10', hour='9-11,16-18')
Huey also provides shortcuts for crontab.daily() and
crontab.hourly() schedules:
@huey.periodic_task(crontab.hourly())
def aggregate_stats():
# This task runs every hour, on the hour.
...
For more information see the following API documentation:
Retrying tasks that fail
To configure a task to be automatically retried, use the retries parameter
of the task() decorator:
import random
@huey.task(retries=2) # Retry the task up to 2 times.
def flaky_task():
if random.randint(0, 1) == 0:
raise Exception('failing!')
return 'OK'
Should the task fail, it will be retried immediately.
To specify a delay between retry attempts, we can add a retry_delay
argument:
@huey.task(retries=2, retry_delay=10)
def flaky_task():
# ...
To grow the delay after each failed attempt, add a retry_backoff
multiplier. The task below is retried after 10 seconds, then 20, then 40:
@huey.task(retries=3, retry_delay=10, retry_backoff=2)
def call_external_service():
# ...
Note
retries, retry_delay and retry_backoff can also be specified
for periodic tasks.
It is also possible to explicitly retry a task from within the task, by raising
a RetryTask exception. When this exception is used, the task will
be retried regardless of whether it was declared with retries. The task’s
remaining retries will not be affected, and the attempt is not recorded as an
error. The Result stays unready until the retry finishes.
@huey.task()
def fetch_api_data(url):
try:
fh = urlopen(url)
except HTTPError:
# Try again in 60 seconds for an HTTP error (500, etc).
raise RetryTask(delay=60)
...
For more information, see the following API documentation:
Error handling
When a task raises an unhandled exception, Huey performs several actions:
The exception and traceback are logged.
The
SIGNAL_ERRORsignal is emitted.An error result is stored in the result store (a dict containing the exception representation, traceback, task ID, and remaining retries).
If the task has an
on_errorhandler, the error handler task is enqueued with the exception as an argument.If the task has retries remaining, it is re-enqueued (and
SIGNAL_RETRYINGis emitted).
Steps 3 and 4 run on every failed attempt by default. To defer them until a task’s retries are exhausted, see Deferring errors until retries are exhausted.
Reading error results
When you call Result.get() on a task that failed, a TaskException
is raised. The TaskException has a metadata attribute containing details
about the failure:
from huey.exceptions import TaskException
@huey.task()
def download(url):
resp = requests.get(url)
resp.raise_for_status()
return resp.text
result = download('https://example.com/missing')
try:
result.get(blocking=True, timeout=10)
except TaskException as exc:
print(exc.metadata['error']) # "HTTPError('404 ...')"
print(exc.metadata['traceback']) # Full traceback string.
print(exc.metadata['task_id']) # UUID of the failed task.
print(exc.metadata['retries']) # Retries remaining (0 if exhausted).
Result handle after a retry
If the task is retried and eventually succeeds, the success result
overwrites the error. However, the Result object caches the
result locally after the first read. To see the updated result after a retry,
you must call reset() (only with the default
store_intermediate_errors=True, see Deferring errors until retries are exhausted):
@huey.task(retries=2, retry_delay=10)
def flaky_download(url):
resp = requests.get(url)
resp.raise_for_status()
return resp.text
result = flaky_download('https://example.com/intermittent')
try:
result.get(blocking=True, timeout=5)
except TaskException:
# First attempt failed. Wait for the retry to finish.
result.reset()
value = result.get(blocking=True, timeout=30)
# value now contains the successful return value (or raises again).
Deferring errors until retries are exhausted
By default a failing task reports its error on every attempt, even those that
still have retries remaining. The exception is written to the result store, and
the on_error handler runs each time. Raising RetryTask, or
being rate-limited or blocked by a lock, is not a failure while a retry is
pending: nothing is stored and on_error does not run.
Pass store_intermediate_errors=False to change this. A failing task that
still has retries is re-enqueued, and nothing is written to the result store or
sent to the on_error handler. The error or return value is stored once the
task succeeds or runs out of retries.
huey = RedisHuey('my-app', store_intermediate_errors=False)
@huey.task(retries=2, retry_delay=10)
def flaky_download(url):
resp = requests.get(url)
resp.raise_for_status()
return resp.text
result = flaky_download('https://example.com/x')
value = result.get(blocking=True, timeout=60)
The SIGNAL_ERROR and SIGNAL_RETRYING signals still fire
on every attempt.
Note
This defaults to True for backwards compatibility. Setting it to
False is recommended, and it may become the default in a future
release. It matches chord, which already waits for retries to
run out before reporting an error.
For more information, see:
Immediate mode
Huey can be run in a special mode called immediate mode, which is very useful during testing and development. In immediate mode, Huey will execute task functions immediately rather than enqueueing them, while still preserving the APIs and behaviors one would expect when running a dedicated consumer process.
Immediate mode can be enabled in two ways:
huey = RedisHuey('my-app', immediate=True)
# Or at any time, via the "immediate" attribute:
huey = RedisHuey('my-app')
huey.immediate = True
To disable immediate mode:
huey.immediate = False
By default, enabling immediate mode will switch your Huey instance to using
in-memory storage. This is to prevent accidentally reading or writing to live
storage while doing development or testing. If you prefer to use immediate mode
with live storage, you can specify immediate_use_memory=False when creating
your Huey instance:
huey = RedisHuey('my-app', immediate_use_memory=False)
You can try immediate mode in the Python shell:
>>> from huey import RedisHuey
>>> huey = RedisHuey()
>>> huey.immediate = True
>>> @huey.task()
... def add(a, b):
... return a + b
...
>>> result = add(1, 2)
>>> result()
3
>>> add.revoke(revoke_once=True) # We can revoke tasks.
>>> result = add(2, 3)
>>> result() is None
True
>>> add(3, 4)() # No longer revoked, was restored automatically.
7
What happens if we try to schedule a task for execution in the future, while using immediate mode?
>>> result = add.schedule((4, 5), delay=60)
>>> result() is None # No result.
True
The task was not executed. Instead it was added to the in-memory storage
layer’s schedule, which we can check by calling Huey.scheduled():
>>> huey.scheduled()
[__main__.add: 8873...bcbd @2019-03-27 02:50:06]
Since immediate mode is fully synchronous, there is not a separate thread monitoring the schedule. The schedule can still be read or written to, but scheduled tasks will not automatically be executed and periodic tasks will not be enqueued.
Testing Guidelines
Set your
Hueyinstance to Immediate mode. Any code that calls a task will run it synchronously, so it is safe to block on results as well.Unit test individual tasks by using the
.call_local()to call the underlying function.
Examples:
# Consider the following Huey instance and tasks:
huey = RedisHuey(...)
# Fake database to check side-effects.
database = []
@huey.task()
def add(a, b):
return a + b
@huey.periodic_task(crontab(...))
def run_reports():
global database
database.append(True) # Simulate a side-effect.
# The return values for periodic tasks are discarded when run by the
# Huey consumer. A return value may be helpful for testing, however, so
# in this example we will return something to demonstrate how to test
# our periodic task.
return 42
class TestMyTasks(unittest.TestCase):
def setUp(self):
# Make tasks run synchronously.
huey.immediate = True
def tearDown(self):
huey.immediate = False
def test_task(self):
result_handle = add(3, 4)
self.assertEqual(result_handle.get(), 7)
# Alternatively, you can also:
self.assertEqual(add.call_local(3, 4), 7)
def test_task_exceptions(self):
# We can also test exceptions.
result_handle = add(3, None) # Exception logged, but not raised.
with self.assertRaises(TaskException):
result_handle.get()
with self.assertRaises(TypeError):
# Exception raised directly when using call_local().
add.call_local(3, None)
def test_periodic_task(self):
# We cannot use the result-handle from a periodic task, because the
# results are always discarded by the consumer. In this case it is
# necessary to use `.call_local()` if we want to check the return
# value.
self.assertEqual(run_reports.call_local(), 42)
self.assertEqual(len(database), 1)
# If our periodic task has a side-effect, however, we can call it
# normally and check the side-effect happened. For example, if the
# run_reports() periodic task wrote a row to a database, we could
# do something like:
run_reports()
self.assertEqual(len(database), 2)
Task priority
Note
Priority support for Redis requires Redis 5.0 or newer. To use task
priorities with Redis, use PriorityRedisHuey instead of
RedisHuey, or PriorityRedisExpireHuey instead of
RedisExpireHuey.
Every other storage layer supports task priorities natively:
SqliteHuey, CySqliteHuey,
PostgresHuey, FileHuey, and the in-memory storage
layer used when Immediate mode is enabled.
Huey tasks can be given a priority, allowing you to ensure that your most important tasks do not get delayed when the workers are busy.
Priorities can be assigned to a task function, in which case all invocations of
the task will default to the given priority. When no priority is given, the
task will default to a priority of 0.
@huey.task(priority=10)
def send_email(to, subj, body):
return mailer.send(to, 'webmaster@myapp.com', subj, body)
When we invoke this task, it will be processed before any other pending tasks whose priority is less than 10. So we could imagine our queue looking something like this:
process_payment- priority = 50check_spam- priority = 1make_thumbnail- priority = 0 (default)
Invoke the send_email() task:
send_email('new_user@foo.com', 'Welcome', 'blah blah')
Now the queue of pending tasks would be:
process_payment- priority = 50send_email- priority = 10check_spam- priority = 1make_thumbnail- priority = 0
We can override the default priority by passing priority= as a keyword
argument to the task function:
send_email('boss@mycompany.com', 'Important!', 'etc', priority=90)
Now the queue of pending tasks would be:
send_email(to boss) - priority = 90process_payment- priority = 50send_email- priority = 10check_spam- priority = 1make_thumbnail- priority = 0
Task priority only affects the ordering of tasks as they are pulled from the
queue of pending tasks. If there are periods of time where your workers are not
able to keep up with the influx of tasks, Huey’s priority feature can
ensure that your most important tasks do not get delayed.
Task-specific priority overrides can also be specified when scheduling a task to run in the future:
# Uses priority=10, since that was the default we used when
# declaring the send_email task:
send_email.schedule(('foo@bar.com', 'subj', 'msg'), delay=60)
# Override, specifying priority=50 for this task.
send_email.schedule(('bar@foo.com', 'subj', 'msg'), delay=60, priority=50)
Lastly, we can specify priority on periodic_task:
@huey.periodic_task(crontab(minute='0', hour='*/3'), priority=10)
def some_periodic_task():
# ...
For more information:
Canceling or pausing tasks
Any task can be canceled (“revoked”), provided the task has not started executing yet. Similarly, a revoked task can be restored, provided it has not already been processed and discarded by the consumer.
Using the Result.revoke() and Result.restore() methods:
# Schedule a task to execute in 60 seconds.
res = add.schedule((1, 2), delay=60)
res.revoke()
# We can check to see if the task is revoked.
res.is_revoked() # -> True
res.restore()
To revoke all instances of a given task, use the
revoke() and restore() methods on
the task function itself:
add.revoke()
# We can check to see that all instances of the add() task
# are revoked:
add.is_revoked() # -> True
# We can enqueue an instance of the add task, and then check
# to verify that it is revoked:
res = add(1, 2)
res.is_revoked() # -> True
# To re-enable a task, we'll use the restore() method on
# the task function:
add.restore()
# Is the add() task enabled again?
add.is_revoked() # -> False
For more information, see the following API docs:
Result.revoke()andResult.restore()for revoking individual instances of a task.Result.is_revoked()for checking the status of a task instance.TaskWrapper.revoke()andTaskWrapper.restore()for revoking all instances of a task.TaskWrapper.is_revoked()for checking the status of the task function itself.
Canceling from within a Task
Huey provides a CancelExecution exception which can be raised,
either within a pre_execute() hook or within the body of a
task()-decorated function, to cancel the execution of the task.
@huey.task(retries=2)
def load_data():
if something_temporary_is_wrong():
# Task will be retried, even if it has run out of retries or is a
# task that does not specify any automatic retries.
raise CancelExecution(retry=True)
elif something_fatal_is_wrong():
# Task will NOT be retried, even if it has more than one retry
# remaining.
raise CancelExecution(retry=False)
elif cancel_and_maybe_retry():
# Task will only be retried if it has one or more retries
# remaining (this is the default).
raise CancelExecution()
...
For more information, see: CancelExecution.
Canceling or pausing periodic tasks
The revoke() method accepts two optional parameters:
revoke_once- boolean flag, if set then only the next occurrence of the task will be revoked, after which it will be restored automatically.revoke_until- datetime, which specifies the time at which the task should be automatically restored.
For example, suppose we have a task that sends email notifications, but our mail server goes down and won’t be fixed for a while:
@huey.periodic_task(crontab(minute='0', hour='*'))
def send_notification_emails():
# ... code to send emails ...
Here is how we might revoke the task for the next 3 hours:
>>> now = datetime.datetime.now()
>>> eta = now + datetime.timedelta(hours=3)
>>> send_notification_emails.revoke(revoke_until=eta)
Alternatively, we could use revoke_once=True to just skip the next
execution of the task:
>>> send_notification_emails.revoke(revoke_once=True)
Task expiration
Huey tasks can be configured with an expiration time. Setting an expiration time on tasks will prevent them being run after the given time has elapsed. Expiration times may be useful if your queue is busy and there may be a significant lag between the time a task is enqueued and the time the consumer starts executing it.
Expiration times can be specified as:
datetime()instances, which are treated as absolute times.timedelta()orint, which are relative to the time at which the task is enqueued.
A default expire time can be provided when declaring a task:
# Task must be executed by consumer within 60s of being enqueued.
@huey.task(expires=60)
def time_sensitive_task(...):
Expiration times can be specified per-invocation, as well:
# Task must be executed by consumer within 5 minutes of being enqueued.
time_sensitive_task(report_file, expires=timedelta(seconds=300))
Expiration times can also be specified when scheduling tasks:
# Task scheduled to run in 1 hour, and once enqueued for execution, must be
# run within 60 seconds.
time_sensitive_task.schedule(
args=(report_file,),
delay=timedelta(seconds=3600),
expires=timedelta(seconds=60))
# Example using absolute datetimes instead of relative deltas:
one_hr = datetime.now() + timedelta(seconds=3600)
time_sensitive_task.schedule(
args=(report_file,),
eta=one_hr,
expires=one_hr + timedelta(seconds=60))
Task timeouts
Huey tasks can be interrupted by setting a timeout on the task, in seconds.
The implementation of the timeout mechanism depends on which worker type you
are using in the consumer, and is selected automatically by Huey:
Process: uses
SIGALRM, most robust.Thread: threads do NOT support a hard timeout. To use timeouts with threaded workers, do cooperative timeout-checking from within your task function, described below.
Greenlet: uses
gevent.Timeout, reliable so long as the task yields to the event loop and is not held-up making a blocking call.
A default timeout can be provided when declaring a task:
# Task must finish executing in 60s. If not, a `TaskTimeout` is raised and
# returned to the caller via the result handle.
@huey.task(timeout=60)
def maybe_slow(...):
Timeouts can be specified per-invocation, as well:
# Task will raise a `TaskTimeout` if runs for more than 30s.
maybe_slow(report_file, timeout=30)
Timeouts can also be specified when scheduling tasks:
maybe_slow.schedule(
args=(report_file,),
delay=60,
timeout=120)
When a task times-out, the SIGNAL_TIMEOUT signal will be
fired.
Cooperative Timeout
To implement timeouts with threaded workers, or if you prefer more control within your task function, use the cooperative timeout APIs:
These methods require that the Task instance be passed as context
into the task function with context=True.
Example:
@huey.task(timeout=60, context=True)
def process_report(data, task=None):
for batch in chunk(data, 100):
task.check_timeout() # Will trigger a TaskTimeout if exceeds 60s.
service.process_data(batch) # Some expensive/slow operation.
For budget-aware scheduling you can use the more granular time_remaining:
@huey.task(timeout=60, context=True)
def process_accounts(accounts, task=None):
for i, account in enumerate(accounts):
# If it looks like we might run out of time, schedule the remaining
# accounts to run in 5 minutes.
if task.time_remaining < 10:
process_accounts.schedule(
args=(accounts[i:],),
delay=300)
return
service.process_single_account(account)
Locking tasks
Task locking can be accomplished using the Huey.lock_task() method,
which can be used as a context-manager or decorator.
This lock prevents multiple invocations of a task from running concurrently.
If a second invocation occurs and the lock cannot be acquired, then a
TaskLockedException is raised and the task will not be executed.
If the task is configured to be retried, then it will be retried normally.
Pass ttl (seconds) so a lock left behind by a killed worker expires on its
own. The memory and redis storages support it, others raise
NotImplementedError. With RedisHuey the server must support
hash-field TTL (redis 7.4+ or valkey 9+).
Examples:
@huey.periodic_task(crontab(minute='*/5'))
@huey.lock_task('reports-lock') # Goes *after* the task decorator.
def generate_report():
# If a report takes longer than 5 minutes to generate, we do
# not want to kick off another until the previous invocation
# has finished.
run_report()
@huey.periodic_task(crontab(minute='0'))
def backup():
# Generate backup of code
do_code_backup()
# Generate database backup. Since this may take longer than an
# hour, we want to ensure that it is not run concurrently.
with huey.lock_task('db-backup'):
do_db_backup()
See Huey.lock_task() for API documentation.
Rate-Limiting
Huey provides simple fixed-window rate-limiting for tasks. Rate-limiting behaves much like locking and can be used as a context-manager or a decorator.
Example:
@huey.task()
@huey.rate_limit('data-sync', limit=10, per=60)
def data_sync(data):
# synchronize `data` with another system, but limit to 10 invocations
# in a 60s window.
service.apply_updates(data)
In the above example, if more than 10 calls to data_sync() occur during a
60 second window, the task raises a RateLimitExceeded. This
exception, by default, will cause the task to be retried at the start of the
next rate-limit window - in the example this would be some delay between 0 and
60 seconds. This occurs even if the task has no retries, as above.
Flow:
data_sync()called 10 times in rapid succession, all succeed.data_sync()called again 33 seconds into the rate-limit window.RateLimitExceededis raised. Huey schedules the rate-limited 11th invocation ofdata_sync()to run in 27 seconds when the window resets.data_sync()called again 48 seconds into the rate-limit window.RateLimitExceededraised, 12th invocation ofdata_syncscheduled to run in 12 seconds when the window resets.
This is good for tasks that you must not lose, but which may overwhelm a service if they come in too quickly. There is a risk, however, if the tasks continually exceed the rate-limits, that they “outpace” the rate-limiting and pile up. This can be mitigated by specifying a limited number of retries on the task itself:
@huey.task(retries=2)
@huey.rate_limit('no-thundering-herd', limit=10, per=60, retry=False)
def data_sync(data):
service.apply_updates(data)
This example will behave like the previous example, but will cap each invocation to two retries.
Equivalent example using a context-manager instead of a decorator:
@huey.task(retries=2)
def data_sync(data):
with huey.rate_limit('data-sync', limit=10, per=60, retry=False):
service.apply_updates(data)
See Huey.rate_limit() for API documentation.
Task pipelines
Huey supports pipelines (or chains) of one or more tasks that should be executed sequentially.
The following are equivalent:
result = add(1, 2)
# And:
result = huey.enqueue(add.s(1, 2))
The TaskWrapper.s() method is used to create a Task
instance (which represents the execution of the given function). The
Task is what gets serialized and sent to the consumer.
To create a pipeline, chain additional tasks using the Task.then()
method:
add_task = add.s(1, 2) # Create Task to represent add(1, 2) invocation.
pipeline = (add_task
.then(add, 3) # Call add() with previous result (1+2) and 3.
.then(add, 4) # Previous result ((1+2)+3) and 4.
.then(add, 5)) # Etc.
result_group = huey.enqueue(pipeline)
# Print results of above pipeline.
print(result_group.get(blocking=True))
# [3, 6, 10, 15]
# Alternatively, we could have iterated over the result group:
for result in result_group:
print(result.get(blocking=True))
# 3
# 6
# 10
# 15
When enqueueing a task pipeline, the return value will be a
ResultGroup, which encapsulates the Result objects for
the individual tasks. ResultGroup can be iterated over or you can
use the ResultGroup.get() method to get all the task return values as
a list.
The return value from the parent task is passed to the next task in the pipeline, and so on.
If the value returned by the parent function is a tuple, then the tuple
will be used to extend the *args for the next task. Likewise, if the
parent function returns a dict, then the dict will be used to fill in any
**kwargs not already given to the next task.
Example of chaining fibonacci calculations:
@huey.task()
def fib(a, b=1):
a, b = a + b, a
return (a, b)
pipe = (fib.s(1)
.then(fib)
.then(fib)
.then(fib))
results = huey.enqueue(pipe)
print(results(True)) # Resolve results, blocking until all are finished.
# [(2, 1), (3, 2), (5, 3), (8, 5)]
For more information, see the following API docs:
Warning
If a pipeline step returns None and Huey was initialized
with store_none=False (the default), the result for that step will
not be written to the result store. When you read the ResultGroup
without blocking, that step’s value will appear as None and be
indistinguishable from “result not ready yet.” A blocking read never
returns for that step.
Pipeline execution is unaffected: the next task in the chain still runs,
but it receives no additional arguments from the None-returning step.
If your pipeline steps may legitimately return None and you need to
read results from the ResultGroup, initialize Huey with
store_none=True or (preferable) have your tasks return a sentinel value
instead.
Error pipelines
The Task.error() method chains a task that runs when the parent fails.
The exception is passed as the first argument to the error handler:
@huey.task()
def download(url):
resp = requests.get(url)
resp.raise_for_status()
return resp.text
@huey.task()
def on_download_error(exc, url=None):
logger.error('Download of %s failed: %s', url, exc)
send_alert('Download failed', str(exc))
# Build a task with an error handler.
task = download.s('https://example.com/data')
task.error(on_download_error, url='https://example.com/data')
result = huey.enqueue(task)
Error handlers can also be combined with success pipelines:
task = (download.s('https://example.com/data')
.then(parse_response)
.then(store_result))
task.error(on_download_error)
result = huey.enqueue(task)
If download raises an exception, on_download_error is called and the
success pipeline (parse_response, store_result) is skipped.
When a stage fails with no retries remaining, its error is also written as the
result of every skipped downstream stage, so their Result handles
raise TaskException instead of blocking. The task_id in the
exception metadata is the id of the stage that failed.
Note
The error handler is attached to the first task in the pipeline. If a
later task in the pipeline fails (e.g., parse_response), the error
handler on download is not triggered. To handle errors in later stages,
attach error handlers to those tasks directly.
For more information, see:
Groups and Chords
Groups and chords allow you to run tasks in parallel, optionally collecting their results and passing them to a final callback.
group - parallel execution
group allows you to enqueue one or more tasks in parallel and
gather the results. All tasks are enqueued immediately. A
ResultGroup is returned to enable fetching the results of the tasks
individually, as they become available, or after they are all completed.
from huey import group
@huey.task()
def fetch(url):
return requests.get(url).json()
urls = ['https://.../1/', 'https://.../2/', ...]
result_group = huey.enqueue(group([
fetch.s(url) for url in urls]))
# Block until all tasks are done.
results = result_group.get(blocking=True)
# [{'id': 1, ...}, {'id': 2, ...}, ...]
Unlike map(), groups may contain different types of
tasks:
@huey.task()
def check_cache():
return cache.ping()
@huey.task()
def check_db():
try:
return db.execute('select 1').fetchone() is not None
except Exception:
logger.exception('database health-check failed')
return False
g = group([check_cache.s(), check_db.s()])
result_group = huey.enqueue(g)
cache_ok, db_ok = result_group(blocking=True)
Error handlers can be attached to every member of a group:
@huey.task()
def on_fetch_error(exc):
logger.error('Fetch failed: %s', exc)
g = group([fetch.s(u) for u in urls]).error(on_fetch_error)
chord - map / reduce
chord is a group with a callback. The member tasks are
run in parallel and when all tasks are finished, their results are passed to
the callback task in order.
from huey import chord
@huey.task()
def fetch(url):
return (url, requests.get(url).text)
@huey.task()
def index_pages(results):
for url, html in results:
search.index(url, html)
return len(results)
urls = ['https://a.com', 'https://b.com', ...]
c = chord(
[fetch.s(url) for url in urls],
index_pages)
result = huey.enqueue(c)
pages = result(blocking=True)
print('Indexed %s pages' % pages)
What happens when a chord is enqueued?
All sub-tasks are placed on the queue. Workers begin executing them as they are dequeued.
As each sub-task completes (or fails permanently), its return value (or error) is stored and an internal completion counter is atomically incremented.
When the last sub-task is complete, all sub-task results are collected in order. The final callback is then enqueued with the sub-task results.
The callback is executed by a worker and the final result is made available.
Note
A sub-task that is skipped without executing (because it was revoked,
expired, or cancelled by a pre-execute hook) still counts towards chord
completion and contributes huey.SKIPPED as its result. A sub-task
that is interrupted by an abrupt consumer shutdown, however, is lost
(tasks are delivered at-most-once), in which case the callback will not
fire.
Enqueueing a chord returns a ChordResult:
c = chord([fetch.s(u) for u in urls], index_pages).then(send_report)
result = huey.enqueue(c)
result() # Get result of index_pages (chord callback).
result.results # ResultGroup for the sub-tasks (fetch(...)).
result.callback # Result handle for callback.
result.pipeline_results # ResultGroup for [index_pages, send_report]
Composing groups and chords
Groups and chords allow the same composition APIs as pipelines. The following are equivalent:
@huey.task()
def incr(n):
return n + 1
@huey.task()
def total(ns):
return sum(ns)
result = huey.enqueue(
group([incr.s(i) for i in range(10)])
.then(total))
result = huey.enqueue(
chord([incr.s(i) for i in range(10)],
total))
Using then() or error() on a chord
will attach an additional callback to the chord’s callback:
result = huey.enqueue(
group([incr.s(i) for i in range(3)])
.then(total)
.then(incr)
.error(alert_admin))
# Chord result is the output of total().
print(result(blocking=True)) # 1 + 2 + 3 == 6.
# Results of chord pipeline are 6, 7.
print(result.pipeline_results(blocking=True)) # [6, 7]
If total raises an exception, alert_admin is executed instead.
Pipelines inside chords
Individual chord members can be pipelines:
@huey.task()
def incr(n):
return n + 1
@huey.task()
def total(ns):
return sum(ns)
result = huey.enqueue(chord(
[incr.s(i).then(incr).then(incr) for i in range(3)],
total))
# First runs incr(0), incr(1), incr(2).
# Then: runs incr(1), incr(2), incr(3).
# Then: runs incr(2), incr(3), incr(4).
# Then: runs total([3, 4, 5])
print(result(True)) # 12.
The chord callback is not enqueued until all sub-task pipelines have completed.
chords inside chords
chords can also contain chords:
@huey.task()
def fetch(url):
return requests.get(url).json()
@huey.task()
def merge(results):
combined = {}
for r in results:
combined.update(r)
return combined
@huey.task()
def publish(merged):
database.write(merged)
inner1 = chord([fetch.s(u) for u in us_urls], merge.s())
inner2 = chord([fetch.s(u) for u in eu_urls], merge.s())
result = huey.enqueue(
chord([inner1, inner2], publish.s()))
When both inner merge tasks complete, their results are collected and
passed to publish.
Error handling in chords
chords are designed to always complete. Each entry in the results list passed to the callback is one of:
the sub-task’s return value.
huey.Errorif the sub-task failed with no retries remaining. Itsmetadatadict holds theerrorrepr,traceback,retriesandtask_id.huey.SKIPPEDif the sub-task never ran (revoked, expired, or cancelled by a pre-execute hook).
from huey import Error, SKIPPED
@huey.task()
def aggregate(results):
if any(isinstance(r, Error) for r in results):
logger.warning('One of the sub-tasks failed.')
return process([r for r in results
if r is not SKIPPED and not isinstance(r, Error)])
The chord will not receive an Error unless the sub-task has
exhausted available retries.
@huey.task(retries=3, retry_delay=10)
def flaky_fetch(url):
return requests.get(url).json()
c = chord(
[flaky_fetch.s(u) for u in urls],
aggregate.s())
If you prefer to handle errors before they reach the callback, catch exceptions inside your sub-tasks and return a sentinel value:
@huey.task()
def safe_fetch(url):
try:
return requests.get(url).json()
except Exception:
return None
@huey.task()
def aggregate(results):
valid = [r for r in results if r is not None]
return process(valid)
Error callbacks on members work independently of the chord and can be
applied via group.error():
@huey.task()
def on_fetch_error(exc):
logger.error('Fetch failed: %s', exc)
tasks = [fetch.s(u).error(on_fetch_error) for u in urls]
c = chord(tasks, aggregate.s())
# Or equivalently using group.error():
c = (group([fetch.s(u) for u in urls])
.error(on_fetch_error)
.then(aggregate))
Error callbacks on the chord itself apply to the callback task:
c = chord(
[fetch.s(u) for u in urls],
aggregate.s()
).error(alert_admin)
In this example, alert_admin runs if aggregate raises an exception,
not if a member fails.
Note
Revoking a chord member will prevent it from running, but the chord will still complete. If you need to cancel a chord, revoke the callback task instead.
Note
Unlike regular tasks, the value a chord member hands to its callback is
recorded even when it is None, so the callback receives None
regardless of the store_none setting. The member’s own
Result, reached through ChordResult.results, still obeys
store_none.
Dynamic fan-out
Because chord members must be known at enqueue time, dynamic fan-out (where the set of subtasks depends on a runtime value) should be done from inside a task:
@huey.task()
def discover_and_fetch(config):
urls = compute_urls(config)
result = huey.enqueue(chord(
[fetch.s(u) for u in urls],
aggregate.s()))
return result.callback.id # Return the callback task id for tracking.
For more information, see the following API docs:
Signals
The Consumer sends signals as it processes tasks.
The Huey.signal() method can be used to attach a callback to one or
more signals.
@huey.signal()
def print_signal_args(signal, task, exc=None):
if signal == SIGNAL_ERROR:
print('%s - %s - exception: %s' % (signal, task.id, exc))
else:
print('%s - %s' % (signal, task.id))
The signal() method accepts zero or more signals as positional
arguments. If none are provided, the handler will be called for any signal.
The callback function (print_signal_args) accepts two required arguments,
which are present on every signal: signal and task. Additionally, our
handler accepts an optional third argument exc which is only included with
SIGNAL_ERROR.
Warning
Signal handlers are executed synchronously by the consumer, so it is typically a bad idea to introduce any slow operations into a signal handler.
Huey emits the following signals:
SIGNAL_ENQUEUED- task has been placed on the queue.SIGNAL_EXECUTING- task is about to be executed by a worker.SIGNAL_COMPLETE- task finished successfully.SIGNAL_ERROR- task raised an unhandled exception.SIGNAL_CANCELED- task was canceled viaCancelExecution.SIGNAL_RETRYING- task failed but will be retried.SIGNAL_SCHEDULED- task was added to the schedule for future execution.SIGNAL_REVOKED- task was revoked and will not be executed.SIGNAL_EXPIRED- task’s expiration time passed before execution.SIGNAL_LOCKED- task could not acquire its lock.SIGNAL_TIMEOUT- task exceeded its execution timeout.SIGNAL_RATE_LIMITED- task was rate-limited.SIGNAL_INTERRUPTED- consumer was shut down while the task was still executing.
All signals are available in the huey.signals module. See the
Signals document and the Huey.signal() API documentation.
Consumer shutdown and interrupted tasks
When the consumer is shut down gracefully (SIGINT / Ctrl+C), it finishes
executing any in-progress tasks before exiting. However, if the consumer is
killed with SIGTERM or crashes, tasks that are mid-execution are lost and
will not be retried automatically.
When the consumer shuts down, it calls
Huey.notify_interrupted_tasks(), which emits SIGNAL_INTERRUPTED
for each task that was still executing. You can use this signal to re-enqueue
interrupted tasks:
from huey.signals import SIGNAL_INTERRUPTED
@huey.signal(SIGNAL_INTERRUPTED)
def on_interrupted(signal, task, *args, **kwargs):
# Re-enqueue the task so it will be executed again.
huey.enqueue(task)
Warning
Re-enqueueing interrupted tasks means the task may run more than once. If your task is not idempotent, consider tracking execution state externally (e.g. in a database) rather than blindly re-enqueueing.
Logging
Huey uses the standard library logging module. Messages are logged to the
huey namespace, with consumer-specific messages being logged to
huey.consumer.
When the consumer is run, it binds a default StreamHandler() to the huey
namespace so that all messages are logged to the console. The consumer logging
can be configured using the following consumer options:
-l FILE, --logfile=FILE- log to a file.-v, --verbose- verbose logging (includes DEBUG level)-q, --quiet- minimal logging-S, --simple- simple logging format (“time message”)
If you would like to get email alerts when an error occurs, you can attach a
logging.handlers.SMTPHandler to the huey namespace at level ERROR:
from logging.handlers import SMTPHandler
import logging
mail_handler = SMTPHandler(
mailhost=('smtp.gmail.com', 587),
fromaddr='errors@myapp.com',
toaddrs=['developers@myapp.com'],
subject='Huey error log',
credentials=('errors@myapp.com', 'secret_password'),
secure=())
mail_handler.setLevel(logging.ERROR)
logging.getLogger('huey').addHandler(mail_handler)
Storage Options
RedisHueyHuey’s capabilities are, to a large extent, informed by the functionality available in Redis. This is the most robust option available and can handle very busy workloads. Because Redis runs as a separate server process, it is even possible to run Huey consumers on multiple machines to facilitate “scale-out” operation.
Operations are guaranteed to be atomic. The queue is stored in a Redis list, scheduled tasks use a sorted set, and the task result-store is kept in a hash.
Tasks that return a meaningful value rely on the caller “resolving” those return values at some point, so the result store does not fill up with unused data. Tasks whose result you never read should return
None.By default Huey performs a “blocking” pop on the queue, which reduces latency, although polling can be used instead by passing
blocking=Falsewhen instantiatingRedisHuey.For low-latency result-fetching, you can specify
notify_result=Truewhen instantiatingRedisHuey. This works by coordinating result readiness through a blocking pop on a dedicated result list. By default this result-readiness list will expire after 60 seconds, but this can be controlled with thenotify_result_ttlparameter. All RedisHuey implementations support these options.Task priorities are not supported by
RedisHuey.PriorityRedisHueyRedis storage layer that supports task priorities.
PriorityRedisHueystores the queue in a sorted set. Since sorted sets require unique keys, Huey uses the timestamp in microseconds to differentiate tasks enqueued with the same priority.RedisExpireHueyRedis storage layer that stores each task result in its own top-level key with an expiration time, so the result-store does not fill up with unresolved result values. The default expire time is 86400 seconds, set with the
expire_timeparameter during instantiation.PriorityRedisExpireHueyCombines the behaviors of
PriorityRedisHueyto support task priorities, with the result-store expiration behavior ofRedisExpireHuey.SqliteHueySqlite works well for many workloads (see Appropriate uses for Sqlite), and Huey’s Sqlite storage layer works well regardless of the worker-type chosen. Sqlite locks the database during writes. Writes generally happen very quickly, however, so in practice this is rarely an issue. Because the database is stored in a single file, taking backups is simple.
SqliteHueymay be a good choice for moderate workloads where the operational complexity of running a separate server process like Redis is undesirable.CySqliteHueySame storage layout and characteristics as
SqliteHuey, but driven by cysqlite rather than the standard librarysqlite3module. Connection tuning is done with an open-endedpragmasdict instead of a fixed set of parameters.PostgresHueyWorkers dequeue using
select ... for update skip locked, so any number of consumers, on any number of hosts, can share a queue without contending for the same rows.LISTEN/NOTIFYwakes an idle worker as soon as a task is enqueued, so there is no polling latency. Huey’s four tables can live in the application’s existing database, which means the queue is backed up and restored along with everything else.Each worker holds one additional connection for
LISTEN, so a consumer with N worker threads uses N+1 connections, and one with N worker processes uses 2N+1. Connection poolers that multiplex (PgBouncer in transaction-pooling mode) breakLISTEN. Initialize withblocking=Falseto fall back to polling in that case.PostgresHueyis a good choice when your application already runs Postgres and you would rather not operate a separate Redis server.FileHueyStores the queue, schedule and task results in files on the filesystem. This implementation is provided mostly for testing and development. An exclusive lock is used around all file-system operations, since multiple operations (list directory, read file, unlink file, e.g.) are typically required for each storage primitive (enqueue, dequeue, store result, etc).
MemoryHueyIn-memory implementation of the storage layer used for Immediate mode.
BlackHoleHueyAll storage methods are no-ops.
Tips and tricks
To call a task-decorated function in its original form, you can use
call_local():
@huey.task()
def add(a, b):
return a + b
add.call_local(3, 4) # Returns 7.
The following two examples are equivalent:
@huey.task()
def add(a, b):
return a + b
# Equivalent to:
def _add(a, b):
return a + b
add = huey.task()(_add)
Task functions can be applied multiple times to a list (or iterable) of
parameters using the map() method:
>>> @huey.task()
... def add(a, b):
... return a + b
...
>>> params = [(i, i ** 2) for i in range(10)]
>>> result_group = add.map(params)
>>> result_group.get(blocking=True)
[0, 2, 6, 12, 20, 30, 42, 56, 72, 90]
Retrieving Results by Task ID
If you have a task ID but not the original Result handle, you can
retrieve the result using Huey.result():
# In a web handler, store the task ID when enqueuing:
result = process_upload(file_id)
session['task_id'] = result.id
# Later, in a status-checking endpoint:
task_id = session['task_id']
try:
value = huey.result(task_id, blocking=False, preserve=True)
except TaskException as exc:
return {'status': 'error', 'detail': exc.metadata['error']}
if value is None:
return {'status': 'pending'}
return {'status': 'complete', 'value': value}
Schedule Shorthand
The schedule() method accepts a delay (int/float or
timedelta) or an ETA (datetime):
# These are all equivalent:
add.schedule(args=(1, 2), delay=60)
add.schedule(args=(1, 2), delay=timedelta(seconds=60))
add.schedule(args=(1, 2), eta=datetime.now() + timedelta(seconds=60))
Iterating Results as They Complete
When working with a ResultGroup (from map()
or group), you can iterate over results as they become available
using as_completed():
result_group = huey.enqueue(group([
fetch.s(url) for url in urls]))
for value in result_group.as_completed():
process(value)
Reserved Keyword Arguments
When calling a task-decorated function or using s(), the
following keyword arguments are intercepted by Huey and will not be passed
to your task function:
idetadelayretriesretry_delayretry_backoffpriorityexpirestimeout
@huey.task()
def bad_example(priority=None): # "priority" is reserved!
...
# This will set the task's priority to 10, NOT pass priority=10 to
# the function.
bad_example(priority=10)
If you need a parameter with one of these names, rename it or pass it positionally.
Reading more
Below are links for details on other aspects of the APIs:
Huey- responsible for coordinating executable tasks and queue backendsHuey.task()- decorator to indicate an executable task.Result- handle for interacting with a task.Huey.periodic_task()- decorator to indicate a task that executes at periodic intervals.crontab()- define what intervals to execute a periodic command.For information about managing shared resources like database connections, refer to the shared resources document.
Also check out the notes on running the consumer.