Python user input inside infinite loop too slow, easily confused
<p>I have a Python script running on a Raspberry Pi that sits waiting for user input and records the input in a SQLite database:</p>

<pre><code>#!/usr/bin/env python

import logging
import db

while True:
    barcode = raw_input("Scan ISBN: ")
    if ( len(barcode) &gt; 1 ):
        logging.info("Recording scanned ISBN: " + barcode)
        print "Recording scanned ISBN: " + barcode
        db.recordScan(barcode, 1)
</code></pre>

<p>That <code>db.recordScan()</code> method looks like this:</p>

<pre><code># Adds an item to queue
def recordScan(isbn, shop_id):
    insert = "INSERT INTO scans ( isbn, shop_id ) VALUES ( ?, ? )"
    conn = connect()
    conn.cursor().execute(insert, [isbn, shop_id])
    conn.commit()
    conn.close()
</code></pre>

<p><em>(Note: The whole code repo is available at <a href="https://github.com/martinjoiner/bookfetch-scanner-python/" rel="nofollow">https://github.com/martinjoiner/bookfetch-scanner-python/</a> if you wanna see how I'm connecting to the db and such)</em> </p>

<p>My problem is that using a USB barcode scanner (which is effectively just a keyboard input that sends a series of keystrokes followed by the <code>Enter</code> key) it is really easy to input at such a fast rate that the command line seems to get <em>"confused"</em>. </p>

<p><strong>For example compare the following results...</strong> </p>

<p>When you go slow the script works well and the command looks neat like this:</p>

<pre><code>Scan ISBN: 9780465031467
Recording scanned ISBN: 9780465031467
Scan ISBN: 9780141014593
Recording scanned ISBN: 9780141014593
Scan ISBN: 
</code></pre>

<p>But when you hammer it hard and go really fast the input prompt kind of gets ahead of itself and the messages printed by the script get written on top of the input prompt:</p>

<pre><code>Recording scanned ISBN: 9780141014593
9780141014593
9780141014593
9780465031467
Recording scanned ISBN: 9780141014593
Scan ISBN: Recording scanned ISBN: 9780141014593
Scan ISBN: Recording scanned ISBN: 9780141014593
Scan ISBN: Recording scanned ISBN: 9780465031467
Scan ISBN: 9780571273188
9780141014593
</code></pre>

<p>It sometimes hangs in that position indefinitely, I don't know what it's doing but you can wake it back up again with another input and it carries on as normal although the input before the one it hung on doesn't get recorded which is bad because it makes the whole system unreliable. </p>

<p>My question is: Is this an inevitability that I just have to live with? Will I always be able to out-pace the low-powered Raspberry Pi by hitting it with too many inputs in close succession or is there some faster way of doing this? Can I push the database write operation to another thread or something along those lines? Forgive my ignorance, I am learning. </p>

<p>After much experimenting based on helpful advice from users @tomalak, @rolf-of-saxony and @hevlastka my conclusion is that <strong>yes, this <em>is</em> an inevitability that I just have to live with.</strong> </p>

<p>Even if you strip the example down to the basics by removing the database write process and making it a simple <em>parrot</em> script that just repeats back inputs (See <a href="http://stackoverflow.com/questions/40156905/python-on-raspberry-pi-user-input-inside-infinite-loop-misses-inputs-when-hit-wi">Python on Raspberry Pi user input inside infinite loop misses inputs when hit with many</a>), it is still possible to scan items so fast that inputs get missed/skipped/ignored. The Raspberry Pi simply cannot keep up. </p>

<p>So my approach will now be to add an audio feedback feature such as a beep sound to indicate to the user when the device is ready to receive the next input. A route I didn't want to go down but it seems my code is the most efficient it can be and we're still able to hit the limits. Responsibility is with the user to not go at breakneck speed and the best we can do a responsible product builders is give them good feedback. </p>

python python-2.7 sqlite raspberry-pi