Python on Raspberry Pi user input inside infinite loop misses inputs when hit with many

2024/10/18 14:49:02

I have a very basic parrot script written in Python that simply prompts for a user input and prints it back inside an infinite loop. The Raspberry Pi has a USB barcode scanner attached for the input.

while True:barcode = raw_input("Scan barcode: ")print "Barcode scanned: " + barcode

When you scan at a "normal" speed it works reliably and the command output looks like this:

Scan barcode: 9780465031467
Barcode scanned: 9780465031467
Scan barcode: 9780007505142
Barcode scanned: 9780007505142

But when you really hammer it with many scans in close succession it is possible to make it miss inputs and the command output looks like this:

Scan barcode: 9780141049113
Barcode scanned: 9780141049113
Scan barcode: 9780465031467
Barcode scanned: 9780465031467
Scan barcode: 9780007505142
9780571273188
Barcode scanned: 9780571273188

Notice how 9780007505142 was input but never printed back. It got lost in the confusion.

See a video demonstration of my test at: https://youtu.be/kdsfdKFhC1M

My question: Is this an inevitability of using a low powered device like a Pi? Will it always be true that a user with a barcode scanner will be able to out-run the hardware's ability to keep up?

Answer

You should probably read from stdin directly using code similar to the following:

import os
import sys
import selectstdin_fd = sys.stdin.fileno()
try:while True:sys.stdout.write("Scan barcode: ")sys.stdout.flush()r_list = [stdin_fd]w_list = list()x_list = list()r_list, w_list, x_list = select.select(r_list, w_list, x_list)if stdin_fd in r_list:result = os.read(stdin_fd, 1024)result = result.rstrip()result = [line.rstrip() for line in result.split('\n')]for line in result:print "Barcode scanned: %s" % line
except KeyboardInterrupt:print "Keyboard interrupt"

This code should handle the case that multiple lines are read at once. The read buffer size is arbitrary and you might have to change it depending on how much data you need to handle.

https://en.xdnf.cn/q/73027.html

Related Q&A

How to append two bytes in python?

Say you have b\x04 and b\x00 how can you combine them as b\x0400?

Pythonic way to write a function which modifies a list?

In python function arguments are passed by object reference. This means the simplest code to modify a list will modify the object itself.a = [1,2,3]def remove_one(b):b.remove(1)remove_one(a) print(a)T…

Trying different functions until one does not throw an exception

I have some functions which try various methods to solve a problem based on a set of input data. If the problem cannot be solved by that method then the function will throw an exception.I need to try t…

Python: Extracting lists from list with module or regular expression

Im trying to extract lists/sublists from one bigger integer-list with Python2.7 by using start- and end-patterns. I would like to do it with a function, but I cant find a library, algorithm or a regula…

Converting hard integral to lambda function with lambdify

I would like to lambdify the function Integral(t**t,(t,0,x)). It works, but my new function, which was returned by lambdify, doesnt return a number but only sympy.integrals.integrals.Integral class. Bu…

python topN max heap, use heapq or self implement?

theres heapq in python, for general usage. i want recording topN(0~20) for 10e7 records.if use heapq, should use - to translate max to min; and recording a min number of bottom, to call heapq.heappushp…

QSortFilterProxyModel returning artificial row

Im using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, Id like to return a first entry which is not present in the original model, that is, its somehow artificial.This i…

@login_required is losing the current specified language

I am using i18n_patterns to internationalize my app and its working except when I click on a link that requires login (a view protected by @login_required decorator), I am being redirected to the login…

Python slow on fetchone, hangs on fetchall

Im writing a script to SELECT query a database and parse through ~33,000 records. Unfortunately Im running into problems at the cursor.fetchone()/cursor.fetchall() phase of things.I first tried iterati…

Pure Python Quadtree Implementation

All,There are a few examples on implementing a quadtree using Python but my question is, does anyone know of a class written in pure python as in a single .py file that I can easily include in my proje…