5

I have a Py3.x GUI app I've been building with Gooey. The app is fully functional, and the GUI works as intended, with the exception of the built-in console/terminal receiving buffered output.

This isn't an issue if I run the .py file with pythonw -u script.py, however I'm now trying to bundle/freeze the tool in to a .app. The bundling process has worked, but there is still the issue of buffered output when the app is opened.

So far, I have found the following, but nothing I've tried has worked:

  • Several threads have suggested using flush=True with print() to write on each call, but logging - which is what I'm interested in here - has no such complementary flag. Consequently, the various monkey-patching solutions in that thread are also no use to me (with the caveat that I don't know the internals of logging well, so perhaps there is a similar approach applied to a logger that could work? Gooey itself, and the packaging process outputs much more logging info than my own calls, and that is still buffered, so it seems that 'global unbuffering' is what's really needed anyway.

  • This thread offers a number of approaches. Indeed, re-opening sys.stdout with a 0 buffer size is the suggested approach, however there is conflicting opinion in the first thread about whether it still works on Python 3 in light of PIP 3116, and certainly in my hands, use of

    nonbuffered_stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
    sys.stdout = nonbuffered_stdout
    

    seems to break the .app altogether.

  • Trying to 'inject' a -u or PYTHONUNBUFFERED=1 into the shebangs also does not seem to work (in this case on MacOS), with or without -S.


So, TL;DR:

How can unbuffered output be achieved in Python 3.x, when the script is not being explicity invoked with python3 -u ?

I'm still trying to put together a MWE, but the use of gooey combined with the packaging process is making this a bit difficult.

Joe Healey
  • 1,232
  • 3
  • 15
  • 34
  • On which OS are you? – user803422 Sep 26 '19 at 19:56
  • Sorry yep, should have said. This is on OSX 10.14. Python via conda in PyCharm. I’ll get module versions and things when I’m back at my computer. – Joe Healey Sep 26 '19 at 20:23
  • This is weird... According to https://stackoverflow.com/questions/16633911/does-python-logging-flush-every-log, logging flushes all output immediately by default. – Legorooj Sep 30 '19 at 00:02
  • is this Q&A https://stackoverflow.com/questions/21071448/redirecting-stdout-and-stderr-to-a-pyqt4-qtextedit-from-a-secondary-thread anyhow close to your problem ? Or https://stackoverflow.com/questions/881696/unbuffered-stdout-in-python-as-in-python-u-from-within-the-program ? – LoneWanderer Sep 30 '19 at 14:51
  • Would https://stackoverflow.com/questions/107705/disable-output-buffering help? Especially the 4th option. – OrdoFlammae Sep 30 '19 at 18:52
  • @OrdoFlammae I’m afraid not. As I mentioned in the OP, setting the buffer to 0 is no longer supported in Py3. I will explore the `Unbuffered()` class suggested in the first answer though thanks. – Joe Healey Oct 02 '19 at 08:23
  • Does this answer your question? [Disable output buffering](https://stackoverflow.com/questions/107705/disable-output-buffering) – wovano Feb 07 '22 at 11:13

0 Answers0