Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

python - What is the meaning of bind = True keyword in celery?

What is the meaning of bind=True in below celery code? When to use it and when not?

@app.task(bind=True)
def send_twitter_status(self, oauth, tweet):
    try:
        twitter = Twitter(oauth)
        twitter.update_status(tweet)
    except (Twitter.FailWhaleError, Twitter.LoginError) as exc:
        raise self.retry(exc=exc)
question from:https://stackoverflow.com/questions/54899320/what-is-the-meaning-of-bind-true-keyword-in-celery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just a small addition to other answers. As already stated, bound tasks have access to the task instance. One use case when this is needed are retries:

@celery.task(bind=True, max_retries=5)
def retrying(self):
    try:
        return 1/0
    except Exception:
        self.retry(countdown=5)

Another use case is when you want to define custom states for your tasks and be able to set it during task execution:

@celery.task(bind=True)
def show_progress(self, n):
    for i in range(n):
        self.update_state(state='PROGRESS', meta={'current': i, 'total': n})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...