Alexei Safronov
Alexei Safronov

Reputation: 81

How to call celery task with class based method?

I have the class:

class Parser:
    def __init__(self):
        self.OM = Omni() # Creates an class object, which makes auth on the web site, for scrapping
    @app.task
    def foo(self, data):
        self.OM.parse(data)

So how can I call task with foo method? Because when I try to do like this, I takes error : Missing argument data. I think it is because calling the method get data as self parameter

prs = Parser()
prs.foo.delay(data)


         

How I can to resolve it?

Upvotes: 1

Views: 3049

Answers (1)

Daniel Hepper
Daniel Hepper

Reputation: 29977

Creating tasks from methods was possible in Celery 3.x, but it was removed in Celery 4.0 because it was too buggy.

I would create a little helper function:

class Parser:
    def __init__(self):
        self.OM = Omni() # Creates an class object, which makes auth on the web site, for scrapping
    
    def foo(self, data):
        self.OM.parse(data)

@app.task
def foo_task(data)
    prs = Parser()
    parser.foo(data)


foo_task.delay(data)

Upvotes: 4

Related Questions