Python on Raspberry Pi user tab inside infinite loop skips inputs on hit with many

I have a very simple parrot script written in Python that just asks for user input and outputs it back to an infinite loop. Raspberry Pi has a USB barcode scanner attached for input.

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

When scanning at "normal" speed, it works reliably, and the output of the command is as follows:

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

But when you really clog it with many scans with close alternation, you can skip it, and the output of the command looks like this:

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

Please note that has 9780007505142been entered but not printed. He was lost in confusion.

Watch a demo of my test video at: https://youtu.be/kdsfdKFhC1M

: , Pi? , - ?

+4
2

, , stdin , , :

import os
import sys
import select

stdin_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"

, . read , , .

+2

, , raw_input() docs , , raw_input . , , , . ( ). : raw_input ? , , python raw_input ? USB-/, , raw_input , ?

+1

Source: https://habr.com/ru/post/1658332/


All Articles