celery中bind = True关键字的含义是什么? [英] What is the meaning of bind = True keyword in celery?

查看:31
本文介绍了celery中bind = True关键字的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

芹菜代码下面的 bind = True 是什么意思?什么时候使用它,什么时候不使用?

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)

推荐答案

仅是其他答案的一小部分.如前所述,绑定任务可以访问该任务实例.重试的一种用例是:

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})

这篇关于celery中bind = True关键字的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆