My Celery tasks require resources that are a bit expensive to setup but can be reused for many tasks on the same thread, but need to be closed when the worker shutdown. (Specifically, they require a fake X display using Xvfb used to render).
I can easily use a threading.local()
variable and only initialize the resource once per thread, but the shutdown process is not so clear. The questions I have are:
You have signals for almost everything in Celery, so in your case, it sounds like you're looking for worker-process-shutdown signal.
I guess you can do something like (pseudo code):
@worker_process_shutdown.connect
def shutdown(*args, **kwargs):
# release your resources here..
pass