Interfacing Arduino with matplotlib animation I've also asked this question over on stack exchange so I'll link the actual question here.

The short story is I have a plot that updates in real time from data collected on an Arduino. The plot updates on a 50ms + overhead interval and I can either choose on the Arduino side to either delay the loop 50ms + compensation or use flush(). I'm running into some problems with the delay option where my program begins to lag (Python side). I'm wondering if flush will work as I expect and hold the output until my python program reads from the serial port. So then the Python program is completely controlling the flow of data every time it polls for an update.

Edit: Here's a snippet of the code.

void loop() {
  // put your main code here, to run repeatedly:
  val1 = analogRead(0);
  val2 = analogRead(1);
  Serial.print(val1);
  Serial.print(" ");
  Serial.print(val2);
  Serial.print("\n");
  //Serial.flush();
  delay(50); 
}
On the Arduino side, flush simply waits until the last byte has been fully transmitted.  It says nothing about how quickly it is being read by the PC.

The general problem you are having is called "flow control".  It has been solved with many different techniques, like "handshaking".  For example, the PC could send a character when it has received the last byte of a data set (a line, as returned by readline):

line = self.ser.readline()
self.ser.write("!"); // Tell Arduino line was received
line = line.split()


While the PC proceeds to parse the line, the Arduino will eventually receive the handshake character:

void loop()
{
  // Send a line
  Serial.println( ... );

  // Wait for handshake character
  bool gotHandshake;
  do {
    while (!Serial.available())
      ; // wait for chars
    char c = Serial.read();
    gotHandshake = (c == '!');
  } while (!gotHandshake);
}


Then the Arduino can proceed to send the next data set (i.e., a line).  The OS on the PC will buffer characters from the Arduino until the python program receives them with readline and sends another handshake character.  No delay required.