rnturn |
07-14-2018 05:33 PM |
What is the Pythonic equivalent to Perl's "$| = 1" ?
I'm pretty sure that the subject line pretty much is all you need, but just in case:
I have a Python (v3.4.5) that runs for a good long time and I've included status messages that I'd like to see displayed during execution that wind up not being displayed until some time when the system sees that you have a buffer full of text to display. So instead of seeing
Code:
Beginning data collection phase... (20 seconds pass) Done. (Took 00:20)
I see
Code:
(20 seconds pass)
Beginning data collection phase... Done. (Took 00:20)
(Actually, it's worse than that. I might see a couple of more "Beginning... Done" messages before the OS buffering decides to finally spit text onto the terminal.)
In ages past, we'd do something like this in our C programs:
Code:
printf( format, args);
fflush(STDOUT);
to get immediate output. In Perl, I can control this with "$| = 1" near the top of the script which causes immediate output rather than letting Perl and the OS wait until there was a buffer full of stuff to display.
Surely Python has a mechanism to force this without having to write a wrapper around prints that (somehow) does a manual flush. Right?
TIA for any tips.
UPDATE: I'm not sure how I missed this when I was poking around in the Python docs but there is a command line switch that is supposed to do, basically, what Perl's "$| = 1" statement does. I've modified my "shebang" line to be:
Code:
#!/usr/bin/python3 -u
Even after that, though, I haven't been able to make it work in my testing. Still plugging away on this.
|