17

Is it possible to set DEBUG=False for only a specific app in Django? Celery has a known memory leak when debugging is enabled. I have a development server where I want Celery to run as a service, without debugging so it doesn't leak memory, but I want the rest of my Django app to use debugging so errors will be shown when testing.

3 Answers 3

38

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
3
  • 3
    No, do it in your settings.py
    – asksol
    Commented Jan 26, 2011 at 16:14
  • 1
    This does not work in Celery 4.x. I updated the path to /usr/celery/bin, but still the hack did not work. I think it has to do with how django settings are imported this way: self._settings = symbol_by_name('django.conf:settings') I did not find a workaround yet.
    – sww314
    Commented Jun 16, 2017 at 15:54
  • @sww314 just check if the conditional is ever true... I'm guessing not.
    – C S
    Commented Jun 16, 2019 at 5:26
6

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

0
1

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.)

1
  • Although unnecessary and not that readable, but this is what I was looking for in my heart of hearts! Relaying on argv is not something I enjoy!
    – aliqandil
    Commented Oct 27, 2019 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.