Why adding arguments to Celery causing error on tests in Python?

909 Views Asked by At

I'm trying to test Celery application here's my code

@celery.task(bind=True, default_retry_delay=30)
def convert_video(gif_url, webhook):
    // doing something awesome
       return
    except Exception as exc:
       raise convert_video.retry(exc=exc)

And in my test I have this.

server.convert_video.apply(args=('some_gif', 'http://www.company.com?attachment_id=123')).get()

After I added bind=True, default_retry_delay=30 I get this error

TypeError: convert_video() takes exactly 2 arguments (3 given)

2

There are 2 best solutions below

0
On BEST ANSWER

To be honest, I've never used celery, but a quick look at their docs for the bind argument says that:

The bind argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance.

You generally would only use this if this were a method on a class, not a stand-alone function. As a method on a class, its first argument would be self.

0
On

you're using the bind parmater which means that the first paramater passed to the function will be the task instance - it effectively makes the function a method of the Task class.

@celery.task(bind=True, default_retry_delay=30)
def convert_video(self, gif_url, webhook):
    try:
        log.info('here we have access to task metadata like id: %s', self.request.id)
        return
    except Exception as exc:
        raise convert_video.retry(exc=exc)