DEV Community

Peter van der Does
Peter van der Does

Posted on

 

Wait for celery to finish its tasks

When deploying a Python application you sometimes have to restart celery. If there is a long-running task you might not want to kill that task, but rather wait 'till the tasks have finished.

This function will pause the accepting of new tasks and wait 'till all tasks are finished.

from celery import Celery
from celery.app.control import Control

def celery_check():
    app = Celery("app") # Name of the main module
    control = Control(app)
    control.cancel_consumer("celery")  # queue name, must probably be specified once per queue, but we use a single queue
    inspect = control.inspect()
    while True:
        active = inspect.active()
        if len(active.items()) > 0:
            seconds = 10
            time.sleep(seconds)
        else:
            break
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Super Useful CSS Resources

A collection of 70 hand-picked, web-based tools which are actually useful.
Each will generate pure CSS without the need for JS or any external libraries.