celery expires 让celery任务具有时效性

来源:互联网 发布:廊坊市北商网络 编辑:程序博客网 时间:2024/06/05 09:25

起因:有的时候,我们希望任务具有时效性,比如定时每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]



能够看到任务已经被撤销了。





0 0
原创粉丝点击