你正在阅读 Celery 3.1 的文档。开发版本文档见: 此处.

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

注解

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)[源代码]
celery.contrib.methods.task(*args, **kwargs)[源代码]

上一个主题

celery.contrib.rdb

下一个主题

celery.events

本页