Running “unique” tasks with celery

后端 未结 5 1846
后悔当初
后悔当初 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:36

    Using https://pypi.python.org/pypi/celery_once seems to do the job really nice, including reporting errors and testing against some parameters for uniqueness.

    You can do things like:

    from celery_once import QueueOnce
    from myapp.celery import app
    from time import sleep
    
    @app.task(base=QueueOnce, once=dict(keys=('customer_id',)))
    def start_billing(customer_id, year, month):
        sleep(30)
        return "Done!"
    

    which just needs the following settings in your project:

    ONCE_REDIS_URL = 'redis://localhost:6379/0'
    ONCE_DEFAULT_TIMEOUT = 60 * 60  # remove lock after 1 hour in case it was stale
    

提交回复
热议问题