title: Pyserial in_waiting CPU usage
body: <p>I have a large python script with a thread that listens to a serial port and puts new data to a queue whenever it's received. I've been trying to improve the performance of the script, as right now even when nothing is happening it's using ~ 12% of my Ryzen 3600 CPU. That seems very excessive.</p>
  <p>Here's the serial listener thread:</p>
  <pre class="lang-py prettyprint-override"><code>def listen(self):
      &quot;&quot;&quot;
      Main listener
      &quot;&quot;&quot;
      while self.doListen:
          # wait for a message
          if self.bus.ser.in_waiting:
              # Read rest of message
              msg = self.bus.read(self.bus.ser.in_waiting)
              # Put into queue
              self.msgQueue.put_nowait(msg)
  </code></pre>
  <p>I profiled the script using <code>yappi</code> and found that the <code>serial.in_waiting</code> call seems to be hogging the majority of the cycles. See the KCachegrind screenshot below:</p>
  <p><a href="https://i.stack.imgur.com/Dr06r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dr06r.png" alt="KCacheGrind Screenshot" /></a></p>
  <p>I tried the trick <a href="https://stackoverflow.com/questions/1328606/python-pyserial-and-cpu-usage">suggested in this question</a>, doing a blocking <code>read(1)</code> call to wait for data. However <code>read(1)</code> just continuously returns empty data and never actually blocks (and yes, I've made sure my pyserial timeout is set to <code>None</code>)</p>
  <p>Is there a more elegant and CPU-friendly way to wait for data on the serial bus? My messages are of variable length, so doing a blocking <code>read(n)</code> wouldn't work. They also don't end in newlines or any specific terminators, so <code>readline()</code> wouldn't work either.</p>