0

I was just curious what the use of flushing would be in this particular instance, as it has no bearing on whether my app functions or not. The line in question is under the 2nd class Route definition, the "sys.stdout.flush()" line. What would that be used for?

class Route(object):
    def __init__(self):
        self.__route = [] #route information array

    def add_route(self, t): #user generated input array
        self.__route.append(t)
        print t.name
        sys.stdout.flush() #using "flush" method

    def compile_list(self): #route list compiler
        output = "" #sets addition variable
        for route in self.__route: #looping inputs
            output += "<div id='container'><div class='results-container'><span class='title'>Route name: </span>" + "<span class='result'>" + route.name + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop One: </span>" + "<span class='result'>" + route.stop_one + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop One Mileage: </span>" + "<span class='results-container'>" + route.stop_one_mileage + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Two: </span>" + "<span class='results-container'>" + route.stop_two + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Two: </span>" + "<span class='results-container'>" + route.stop_two_mileage + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Three: </span>" + "<span class='results-container'>" + route.stop_three + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Three: </span>" + "<span class='results-container'>" + route.stop_three_mileage + "</span></div><br />"
        return output #output return

#===================== Calculates the average of all three ==========================

    def calc_average(self): #average calculation
        stop_one_mileage = self.__route[0].stop_one_mileage #stop_one_mileage average
        stop_two_mileage = self.__route[0].stop_two_mileage #stop_two_mileage
        stop_three_mileage = self.__route[0].stop_three_mileage #stop_three_mileage
        avg = (int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage))/3 #adds all three and divides by three
        return "<div class='results-container'><span class='title'>Average Mileage: </span><span class='results-container'>" + str(avg) + " miles</span></div>" #returns results


#===================== Calculates total of all three ==========================

    def calc_total(self): #calculates the total of all three
        stop_one_mileage = self.__route[0].stop_one_mileage #stop_one_mileage
        stop_two_mileage = self.__route[0].stop_two_mileage #stop_two_mileage
        stop_three_mileage = self.__route[0].stop_three_mileage #stop_three_mileage
        total = int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage) #sets total of all three input
        return "<div class='results-container'><span class='title'>Total Mileage: </span><span class='results-container'>" + str(total) + " miles</span></div>" + "</div>" #returns results

class FormData(object): #form data object
    def __init__(self):
        self.name = ""
        self.stop_one = ""
        self.__stop_one_mileage = "" #makes sure mileage isn't zero
        self.stop_two = ""
        self.stop_two_mileage = "" #makes sure mileage isn't zero
        self.stop_three = ""
        self.stop_three_mileage = "" #makes sure mileage isn't zero

#===================== Getter/Setter ==========================

        @property #stop_one_mileage getter
        def stop_one_mileage(self):
            return self.__stop_one_mileage #returns the stop_one_mileage

        @stop_one_mileage.setter #stop_one_mileage setter
        def stop_one_mileage(self, m):
            if m <= 0:
                self.__stop_one_mileage = 10 #mileage set to 10 (nice round number)
            else:
                self.__stop_one_mileage = m #returns a value as long as the input is more than zero
D-Dubs
  • 7
  • 1
  • 5

2 Answers2

0

It forces a complete write of the I/O buffer to stdout. This might be important in case of odd race conditions (where the exact timing of the buffer write matters) or premature termination (where without flushing, all information in the buffer would be lost), among other situations.

More information

Usage of sys.stdout.flush() method

https://en.wikipedia.org/wiki/Data_buffer

Community
  • 1
  • 1
imareaver
  • 211
  • 2
  • 5
0

Flush guarantees that the output is to be written to standard output immediately, whereas if you don't flush then it might (potentially) stick around in the buffer for an unspecified amount of time.

Exactly whether the string will stay depends on the output device. Some output devices are line-buffered, so print with a newline will automatically flush the buffer. But this is not true in general, so the flush ensures that it's written out regardless.

For your specific case, whether it's useful depends on whether you care about the possibility that the print may not take effect immediately. Without an explicit flush, in the worst case scenario, the output may not appear until the program terminates. If your program will terminate immediately without requiring user interaction then the flush would not make any difference.

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
  • Ok, that makes sense to me. It seems it would be a more useful function for a project on a larger scale than mine. Maybe for a game of some sort, where time lag could/would be an issue. Or maybe a much larger scale like Amazon or maybe even YouTube, where people can get cranky if something takes more than a millisecond. Thank you so much @Rufflewind I appreciate a detailed answer like that. Also, thank you imareaver. – D-Dubs Dec 02 '14 at 01:02