celery expires 让celery任务具有时效性

起因:有的时候。我们希望任务具有时效性。比方定时每5分钟去抓取某个状态,由于celery队列中的任务可能非常多,等到这个任务被运行时。已经超过了5分钟,那么这个任务的运行已经没有意义。由于下一次抓取已经运行了。

能够进行例如以下设定:

@task(ignore_result=True, expires=900)
def nupdate_influence_by_15min(uid, today=None, if_whole=False):
... ...

expires – Either a int, describing the number of seconds, or a datetime object that describes the absolute time and date of when the task should expire. The task will not be executed after the expiration time.

当任务被取出是超过900 秒,任务会直接revoke , 任务会被跳过,不会被运行

以下是celery的日志

Oct 22 13:53:49 bj-social-celery05 social_celery: [2014-10-21 22:08:03,233: INFO/MainProcess] Got task from broker: social_master.sentiment.tasks.daily_update_wayback_sentiment[fe5f3a82-342d-4173-bd13-60182e88ec4f] expires:[2014-10-21 22:08:59.781945]
... ...
Oct 22 13:53:49 bj-social-celery05 social_celery: [2014-10-21 22:09:15,846: WARNING/MainProcess] Skipping revoked task: social_master.sentiment.tasks.daily_update_wayback_sentiment[fe5f3a82-342d-4173-bd13-60182e88ec4f]



可以看到任务已经被撤销了。





原文地址:https://www.cnblogs.com/blfshiye/p/5244185.html