serial communication dead slow after a while I'm workin on a node for blender to control an arduino. I notice it goes really slow after a while so I isolated the problem.
It is good in maintaining 60 frames a second, then after around 40 seconds (pretty exact), it drops to a pretty exact 1 fps for some reason.

What could be going on?

import serial
import time

ser = serial.Serial('COM3', 115200)

c = 0

while True:
    c+=1
    print("ok ", c)
    numIn = "100n"
    ser.write(numIn.encode('ascii'))
    time.sleep(1./60)


arduino code:

// special thx to  Nick Gammon

const unsigned int MAX_INPUT = 50;


unsigned long delay_time = 300;
unsigned long last_toggle_time = 0;
int current_state = LOW;

void setup () {
  Serial.begin (115200);
  pinMode(12, OUTPUT);

} 


void process_data (const char * data) {
  Serial.println (data);
  delay_time = atoi(data);
} 

void processIncomingByte (const byte inByte) {
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte) {

    case 'n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte
      process_data (input_line);
      input_pos = 0;
      break;

    case 'r':   
      break;

    default:
      if (input_pos &lt; (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

  } 
} 

void loop() {

  while (Serial.available () &gt; 0) {
    processIncomingByte (Serial.read ());
  }

  unsigned long m  = millis();

  if (m &gt; last_toggle_time + delay_time) {
      current_state = current_state == HIGH ? LOW : HIGH;
      digitalWrite(12, current_state);         
      last_toggle_time = m;
  }

}
If you print from arduino to serial and you don't read it then the buffer gets full. Causing it to go in a second delay. So make sure you read as well when you send data over.

A short sleep is required between the sleep and the read for some reason. Else it hangs.

import serial
from time import sleep

ser = serial.Serial('COM3', 115200)
sleep(1)

c = 0

while True:
    c+=1
    print("ok ", c)
    numIn = "100"
    ser.write((numIn + '\r\n').encode())
    sleep(1./120)

    inp = ser.readline()
    print(inp.decode("utf-8"))
    sleep(1./120)