Running “unique” tasks with celery

后端 未结 5 1935
后悔当初
后悔当初 2020-11-28 03:40

I use celery to update RSS feeds in my news aggregation site. I use one @task for each feed, and things seem to work nicely.

There\'s a detail that I\'m not sure to

5条回答
  •  迷失自我
    2020-11-28 04:22

    Based on MattH's answer, you could use a decorator like this:

    def single_instance_task(timeout):
        def task_exc(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                lock_id = "celery-single-instance-" + func.__name__
                acquire_lock = lambda: cache.add(lock_id, "true", timeout)
                release_lock = lambda: cache.delete(lock_id)
                if acquire_lock():
                    try:
                        func(*args, **kwargs)
                    finally:
                        release_lock()
            return wrapper
        return task_exc
    

    then, use it like so...

    @periodic_task(run_every=timedelta(minutes=1))
    @single_instance_task(60*10)
    def fetch_articles()
        yada yada...
    

提交回复
热议问题