-1

My class includes a call to load data and to train a (deep learning) model; in-between, I have print statements for logging purposes:

get_data() -> print() -> train() -> print()

Fitting set 5... (Loss,Acc) = (0.512,0.921)

Fitting set is printed after get_data(), and Loss,Acc after train() - however, adding an additional print statement within get_data(), as below, modifies the output log as follows:

def get_data():
    t0=time()
    data = load_data(path)
    print(time()-t0,'sec')
    return data

Fitting set 5... (Loss,Acc) = (0.512,0.921) 2.486 sec

The time log is printed after train(), despite being called before. Further, the time log is printed immediately after the Loss,Acc log, rather than in '2.etc' secs - so it isn't a call order issue.


How do I force Python to print when it's asked to, rather than later?
OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101
  • find how to deal with output buffering (some tricks could be applied) – RomanPerekhrest Sep 02 '19 at 15:52
  • 3
    Python does print when it's asked to. If the output's in the wrong order, it means that you're either using multithreading (so that multiple threads attempt to write to the same terminal), or it's indeed a call order issue. – ForceBru Sep 02 '19 at 15:54
  • What does the rest of the code look like? It's hard to give a useful answer without a minimal reproducible example (see https://stackoverflow.com/help/minimal-reproducible-example and http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports for more information). – Jasmijn Sep 02 '19 at 15:58
  • Does it actually print all those in the same line, or did you just format it in this way? – tobias_k Sep 02 '19 at 15:59
  • @tobias_k It does, via `end=''` – OverLordGoldDragon Sep 02 '19 at 16:15
  • @ForceBru Unless default Keras uses multithreading, I don't - and no, it isn't a call order issue – OverLordGoldDragon Sep 02 '19 at 16:16
  • 1
    @OverLordGoldDragon, if `flush` seems to help, but _randomly_, as you say in your other comment, it may be an indicator of usage of threads or processes – ForceBru Sep 02 '19 at 16:28

2 Answers2

0

print has a flush parameter that when set to true force the flushing.

https://docs.python.org/3/library/functions.html#print

Booboo
  • 38,656
  • 3
  • 37
  • 60
g.lahlou
  • 468
  • 3
  • 15
  • This works _some_ of the time, and partially at that; time log is delayed by _under_ 2 secs. Most of the time the original behavior persists. – OverLordGoldDragon Sep 02 '19 at 16:14
0

Found what works for me here - pasting:

You can replace sys.stdout with some other stream like wrapper which does a flush after every call:

class Unbuffered(object):
   def __init__(self, stream):
       self.stream = stream
   def write(self, data):
       self.stream.write(data)
       self.stream.flush()
   def writelines(self, datas):
       self.stream.writelines(datas)
       self.stream.flush()
   def __getattr__(self, attr):
       return getattr(self.stream, attr)

import sys
sys.stdout = Unbuffered(sys.stdout)
print('Hello')

Note: I suggest against marking this question as a duplicate, since I could not find an answer to my question with my search words (I didn't know what "output buffering" is, which may apply to others)

OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101