Disable Django Debugging for Celery

笑着哭i 提交于 2019-11-30 06:50:09

Celery doesn't have a memory leak, it's how Django works:

When DEBUG is enabled Django appends every executed SQL statement to django.db.connection.queries, this will grow unbounded in a long running process environment.

I guess you could use a hack like:

if "celeryd" in sys.argv:
    DEBUG = False

The hack from @asksol didn't do it for me, I had to do

if "celery" in sys.argv[0]:
    DEBUG = False

Because sys.argv is something like ['/path/to/celery', 'worker', '-A', 'app']

Im using python 3.6 and celery 4

Building off of asksol's answer (https://stackoverflow.com/a/4806384/3009897) in light of sww314's comment on that answer, that the indicated method does not work in Celery 4.x, one can instead rely on the inspect module to determine the execution stack that the DEBUG setting is being accessed in.

For my purposes, the following change worked:

if DEBUG and 'celery' in inspect.stack()[-1][1]:
    DEBUG = False

This assumes that the outermost invocation frame will include the string 'celery' in the associated filepath. (The check for DEBUG is a stab at ensuring that if this is left in place for production (where DEBUG = False should be set), the inspect shouldn't take place.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!