Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery @task doesn't work with instance methods

I have a class with methods decorate with celery @task like this:

class Port(object): 

    """docstring for Port"""


    def __init__(self,):
        print 'Class has been initialized ...'


    @celery.task(filter=task_method,name="Port.process")    
    def process(self,):
        print "I'm inside the process task method: " 

Called here:

p = Port()

p.process.apply_async()

I also tried: p.process.delay(), with the same below result.

When I run it, I get this error:

[2013-06-22 02:32:53,988: ERROR/MainProcess] Task Port.process[77cff07e-4bc5-4e36-9c4e-b68d7616c74e] raised exception: TypeError('process() takes at least 1 argument (0 given)',) Traceback (most recent call last): File "/usr/local/lib/python2.7/site-packages/celery/task/trace.py", line 228, in trace_task R = retval = fun(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/celery/task/trace.py", line 415, in __protected_call__ return self.run(*args, **kwargs) TypeError: process() takes at least 1 argument (0 given)

This is the important part, TypeError: process() takes at least 1 argument (0 given).

Now how can I solve this??

Some people say this happens because celery uses the method task unbound to the initialized object, and some others say it just works, do I miss something here?

like image 760
securecurve Avatar asked Jun 21 '13 23:06

securecurve


People also ask

Is it possible to use methods as tasks in celery?

Celery has experimental support for using methods as tasks since version 3.0. The documentation for this is in celery.contrib.methods, and also mentions some caveats you should be aware of: Be aware: support for contrib.methods removed from Celery since 4.0 Show activity on this post.

How do I schedule a weather report in celery?

You can write a task to do that work, then ask Celery to run it every hour. The task runs and puts the data in the database, and then your web application has access to the latest weather report. A task is just a Python function. You can think of scheduling a task as a time-delayed call to the function.

Is celery a distributed system?

Since Celery is a distributed system, you can’t know which process, or on what machine the task will be executed. You can’t even know if the task will run in a timely manner. The ancient async sayings tells us that “asserting the world is the responsibility of the task”.

How does celery queue work?

When a task is ready to be run, Celery puts it on a queue , a list of tasks that are ready to be run. You can have many queues, but we'll assume a single queue here for simplicity. Putting a task on a queue just adds it to a to-do list, so to speak.


1 Answers

Celery has experimental support for using methods as tasks since version 3.0.

The documentation for this is in celery.contrib.methods, and also mentions some caveats you should be aware of:

http://docs.celeryproject.org/en/latest/reference/celery.contrib.methods.html

Used this as reference

like image 84
securecurve Avatar answered Sep 21 '22 20:09

securecurve