Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python continuously parse console input

I am writing a little Python script that parses the input from a QR reader (which is seen as a keyboard by the system).

At the moment I am using raw_input() but this function waits for an EOF/end-of-line symbol in order to submit the received string to the program. I am wondering if there is a way to continuously parse the input string and not just in chunks limited by a line end.

In practice: - is there a way in python to asynchronously and continuously parse a console input ? - is there a way to change raw_input() (or an equivalent function) to look for another character in order to submit the string read into the program?

like image 658
mαττjαĸøb Avatar asked Nov 11 '22 16:11

mαττjαĸøb


1 Answers

It seems like you're generally trying to solve two problems:

  1. Read input in chunks
  2. Parse that input asynchronously

For the first part, it will vary greatly based on the specifics of the input function your calling, but for standard input you could use something like

sys.stdin.read(1)

As for parsing asynchronously, there are a number of approaches you could take. Python is synchronous, so you will necessarily have to involve some subprocess calls. Manually spawning a function using the subprocess library is one option. You could also use something like Redis or some lightweight job queue to pop input chunks on and have them read and processed by another background script. Finally, gevent is a very popular coroutine based library for spawning asynchronous processes. Using gevent, this whole set up would look something like this:

class QRLoader(object):
    def __init__(self):
        self.data = []

    def add_data(data):
        self.data.append(data)

        # if self._data constitutes a full QR code
        # do something with data
        gevent.spawn(parse_async)

def parse_async():
    # do something with qr_loader.data

qr_loader = QRLoader()

while True:
    data = sys.stdin.read(1)
    if data:
        qr_loader.add_data(data)
like image 109
goldsmith Avatar answered Nov 15 '22 13:11

goldsmith