While developing I use print() statements (with f-strings) but the not all the output is sent to the console.
When I hit ctrl-c and kill the server - I can see all that is printed but when it’s running I don’t always see every statement that should be printing.
I assume there is some sort of timing issue here - is there a way to force the server that comes with Django to print out all the print() statements?
P.S. Yes I know I should be using logging - but the world is not a perfect place.
I’m guessing you’re using manage.py runserver to run your code during development. If so, I’ve also encountered this in the past.
What has worked for me has been to disable output buffering in the python instance by running manage with the -u parameter. In other words, your command would look something like this:
python -u manage.py runserver
Another similar option would be to set the PYTHONUNBUFFERED environment variable to any non-empty string.
What precisely is the “this” that you are referring to here?
Django does provide a wrapper for stdout & stderr for management commands (see django.core.management.base), but the majority of that code was committed in 2013 / 2014, when support for Python 3 was added and the print command was removed in favor of the print function. This doesn’t exactly make it “new”.
Python also checks to see if stdout is associated with a terminal to determine if print output should be buffered or not - which means the print function can act differently depending upon environmental factors.
In the general case, for example using manage.py runserver from a shell, print output shows up in the console log.
For example, this view:
There’s a flush keyword argument to print(). Did you try that?
I personally haven’t seen this, and it seems like if you can make a clean reproduction of this this should be treated as a very serious bug. Print debugging is the last defense when everything else fails, that must work.