Huey comes with special integration for use with the Django framework. This is to accomodate:
Note
Huey settings are optional. If not provided, Huey will default to using Redis running locally.
All configuration is kept in settings.HUEY. Here is an example:
HUEY = {
'queue': 'huey.backends.redis_backend.RedisBlockingQueue', # required.
'queue_name': 'unique name',
'queue_connection': {'host': 'localhost', 'port': 6379},
# Options for configuring a result store -- *recommended*
'result_store': 'huey.backends.redis_backend.RedisDataStore',
'result_store_name': 'defaults to queue name',
'result_store_connection': {'host': 'localhost', 'port': 6379},
# Options to pass into the consumer when running ``manage.py run_huey``
'consumer_options': {'workers': 4},
}
To run the consumer, use the run_huey management command. This command will automatically import any modules in your INSTALLED_APPS named “tasks.py”. The consumer can be configured by the consumer_options settings.
In addition to the consumer_options, you can also pass some options to the consumer at run-time.
For more information, check the consumer docs.
The task API is a little bit simplified for Django. The function decorators are available in the huey.djhuey module.
Here is how you might create two tasks:
from huey.djhuey import crontab, periodic_task, task
@task()
def count_beans(number):
print '-- counted %s beans --' % number
return 'Counted %s beans' % number
@periodic_task(crontab(minute='*/5'))
def every_five_mins():
print 'Every five minutes this will be printed by the consumer'