/usr/lib/python2.7/dist-packages/celery/contrib/methods.py is in python-celery 3.1.23-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | # -*- coding: utf-8 -*-
"""
celery.contrib.methods
======================
Task decorator that supports creating tasks out of methods.
Examples
--------
.. code-block:: python
from celery.contrib.methods import task
class X(object):
@task()
def add(self, x, y):
return x + y
or with any task decorator:
.. code-block:: python
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 (: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:
.. code-block:: python
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:
.. code-block:: python
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:
.. code-block:: python
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
"""
from __future__ import absolute_import
from celery import current_app
__all__ = ['task_method', 'task']
class task_method(object):
def __init__(self, task, *args, **kwargs):
self.task = task
def __get__(self, obj, type=None):
if obj is None:
return self.task
task = self.task.__class__()
task.__self__ = obj
return task
def task(*args, **kwargs):
return current_app.task(*args, **dict(kwargs, filter=task_method))
|