Opened 6 years ago
Closed 6 years ago
#29533 closed New feature (wontfix)
Flushing stdout in django-admin command
Reported by: | David Ruggles | Owned by: | nobody |
---|---|---|---|
Component: | Core (Management commands) | Version: | 2.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm writing django-admin commands that are used interactively on the command line. According to the documentation, I should use
self.stdout.write
instead of
I ran into a case where I wanted to print multiple dots on a line to provide a rudimentary progress indicator. I set
ending=''
and quickly discovered that the output is buffered. With print I could do
print('.', end='', flush=True)
and it would work. Or I could do
sys.stdout.flush()
so I tried
self.stdout.flush()
with no change. I dug into the code and found that the flush method is simply coming from IOBase and is a pass. So I modified OutputWrapper by adding the following method:
def flush(self): self._out.flush()
This did want I wanted, but could there be unintended consequences? If not, would this be a reasonable patch?
I think we'll instead pursue making management commands use logging (#21429).