Dean Christian Armada
Dean Christian Armada

Reputation: 7364

self parameter error celery

I saw some blogs online where they put self parameters on their celery functions, why is mine causing an error like:

TypeError: xml_gr() takes exactly 1 argument (0 given)

Here is my code:

@periodic_task(run_every=timedelta(seconds=5))
def xml_gr(self):
    ftp = FTP('xxxxx')
    ftp.login(user='xxxxx', passwd='xxxxx')
    x = xml_operation('AGIN', GR_GLOBAL_CURRENT_DATE, ftp, "GR")
    ftp.close()

Upvotes: 0

Views: 2469

Answers (2)

Adrian Muntean
Adrian Muntean

Reputation: 342

In addition to the accepted answer, self is used in celery to bind tasks.

Bound tasks are needed for retries. for accessing information about 
the current task request, and for any additional functionality 
you add to custom task base classes.

So, if you specify @task(bind=True) then you need add self as the first argument. Otherwise, not needed.

Source

Upvotes: 5

anregen
anregen

Reputation: 1602

"self" is used within class member functions. When you call a member function in an instance of the class, the language automatically passes in the class instance as "self". "self" lets you access the members of the class.

class Thing:
    var1 = None
    def set_member(self, value):
        self.var1 = value

    def show_member(self, value):
        return self.var1

Then usage would be

a = Thing()
a.set_member(23)
a.show_member()

And you'd see the response 23. You don't have to pass in the "self" variable explicitly.

When you declare functions outside of a class, there is no reason to use "self".

Upvotes: 0

Related Questions