This document describes the current stable version of Celery (3.1). For development docs, go here.

celery.contrib.methods

Task decorator that supports creating tasks out of methods.

Examples

from celery.contrib.methods import task

class X(object):

    @task()
    def add(self, x, y):
            return x + y

or with any task decorator:

from celery.contrib.methods import task_method

class X(object):

    @app.task(filter=task_method)
    def add(self, x, y):
        return x + y

Note

The task must use the new Task base class (celery.Task), and the old base class using classmethods (celery.task.Task, celery.task.base.Task).

This means that you have to use the task decorator from a Celery app instance, and not the old-API:

from celery import task       # BAD
from celery.task import task  # ALSO BAD

# GOOD:
app = Celery(...)

@app.task(filter=task_method)
def foo(self): pass

# ALSO GOOD:
from celery import current_app

@current_app.task(filter=task_method)
def foo(self): pass

# ALSO GOOD:
from celery import shared_task

@shared_task(filter=task_method)
def foo(self): pass

Caveats

  • Automatic naming won’t be able to know what the class name is.

    The name will still be module_name + task_name, so two methods with the same name in the same module will collide so that only one task can run:

    class A(object):
    
        @task()
        def add(self, x, y):
            return x + y
    
    class B(object):
    
        @task()
        def add(self, x, y):
            return x + y
    

    would have to be written as:

    class A(object):
        @task(name='A.add')
        def add(self, x, y):
            return x + y
    
    class B(object):
        @task(name='B.add')
        def add(self, x, y):
            return x + y
    
class celery.contrib.methods.task_method(task, *args, **kwargs)[source]
celery.contrib.methods.task(*args, **kwargs)[source]

Previous topic

celery.contrib.rdb

Next topic

celery.events

This Page