0

In python, I need to record some results in a file. The results are generated by a function in a loop. The following code shows an example:

with open('result_file', 'w') as file:     
    for i in xrange(10000):
        result = somethingTakesTime()
        file.write(str(result), '\n')

Function somethingTakesTime() is time costly. I would like to check the result_file even the program is still working. However, with the current Python 2.7, I only can get the result after the for loop finish. Is there any method that I can see the result (in result_file) even the code is still working?

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
Ding Ding
  • 295
  • 3
  • 7

1 Answers1

2

When programs write data to files, they usually keep the data in an internal buffer to prevent frequent disk writes (which can slow things down). But if the generation of the data is slower than the disk write would otherwise be, it can sometimes be useful to tell the program that you'd like to flush the data immediately to the file. To do this you'd use the .flush method of the file object.

E.g.

with open('result_file', 'w') as file:     
    for i in xrange(10000):
        result = somethingTakesTime()
        file.write(str(result), '\n')
        file.flush()
Chad S.
  • 6,252
  • 15
  • 25