This document describes Celery 3.0. For development docs, go here.

celery.task

celery.task

This is the old task module, it should not be used anymore, import from the main ‘celery’ module instead. If you’re looking for the decorator implementation then that’s in celery.app.base.Celery.task.

celery.task.task(*args, **kwargs)

Decorator to create a task class out of any callable.

Examples

@task()
def refresh_feed(url):
    return Feed.objects.get(url=url).refresh()

With setting extra options and using retry.

@task(max_retries=10)
def refresh_feed(url):
    try:
        return Feed.objects.get(url=url).refresh()
    except socket.error, exc:
        refresh_feed.retry(exc=exc)

Calling the resulting task:

>>> refresh_feed('http://example.com/rss') # Regular
<Feed: http://example.com/rss>
>>> refresh_feed.delay('http://example.com/rss') # Async
<AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
celery.task.periodic_task(*args, **options)

Decorator to create a task class out of any callable.

Examples

@task()
def refresh_feed(url):
    return Feed.objects.get(url=url).refresh()

With setting extra options and using retry.

from celery.task import current

@task(exchange='feeds')
def refresh_feed(url):
    try:
        return Feed.objects.get(url=url).refresh()
    except socket.error, exc:
        current.retry(exc=exc)

Calling the resulting task:

>>> refresh_feed('http://example.com/rss') # Regular
<Feed: http://example.com/rss>
>>> refresh_feed.delay('http://example.com/rss') # Async
<AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
class celery.task.Task

Deprecated Task base class.

Modern applications should use celery.Task instead.

Previous topic

celery.app.utils

Next topic

celery.task.base (Deprecated)

This Page