I have implemented a non-blocking reader in Python and I need to make it more efficient.
Background: I have a massive amount of output that I need to read from one subprocess (started with Popen ()) and go to another thread. Reading the output from this subprocess should not be blocked for more than a few ms (preferably within as little time as possible to read the available bytes).
I currently have a utility class that accepts a file descriptor (stdout) and a timeout. I select() and readline(1) until one of three events occurs:
- I read a new line
- my timeout (several ms) expires
- select tells me not to read anything in this file descriptor.
Then I return the buffered text to the calling method, which does something with it.
Now, for the real question: because I read so many results, I need to make it more efficient. I would like to do this by requesting a file descriptor how many bytes are expected, and then readline([that many bytes]) . It should just convey material, so it doesn't matter to me where the new lines are, or even if they are. Can I ask the file descriptor how many bytes it has for reading, and if so, how?
I have done several searches, but it is very difficult for me to understand what to look for, not to mention whether this is possible.
Even a useful point in the right direction would be useful.
Note. I am developing Linux, but it does not matter for the Pythonic solution.
source share