"Real-time" (since it's obviously "soft" real-time given you have multiple processes going, not "hard" real-time!) does not mean "you cannot use time.sleep
": even a tiny amount of sleep will make things a little bit better -- try adding a time.sleep(0.01)
in your loop, just to give other processes a better chance to run. The lack of sleep may actually be making you take a longer time by giving other processes very little chance to fill the pipe!
Beyond this, @S.Lott has it just right: for "real-timeoid" behavior, you have to read from sys.stdin
(though it probably need not be a byte at a time, depending on the platform: typically sys.stdin.read(1024)
will read up to 1024 bytes, when sys.stdin
is a pipe or other "raw" as opposed to "cooked" FD, returning however many bytes are in the pipe, if <100, rather than waiting -- you can set the FD to non-blocking to help assure that) directly, and perform string manipulation (e.g. to put lines together, strip them, etc) later in your code.