1

Possible Duplicate:
Non-blocking read on a subprocess.PIPE in python

Is it possible to test whether a Python file-like object has charaters after the current read position?

I'm trying to keep a subprocess.PIPE flushed to my stdout, so am continuously running read(1) on it. I only want to read if there will actually be something there for me to get (otherwise it hangs until something shows up).

Community
  • 1
  • 1
ajwood
  • 18,227
  • 15
  • 61
  • 104
  • 2
    But by default these pipes are blocking, this means that when you do `read(1)` and there is nothing to read, it will automatically block until there is something to read. – lvella Sep 27 '12 at 20:41
  • If you're writing code that doesn't have to run on Windows, `select.select` or `select.poll` is very easy, and probably better than anything else. If it has to work on Windows as well, you can write separate Windows and POSIX code, or a cross-platform async event engine, but the simplest answer might be to just spawn a thread that does a blocking read, and signal between the threads with a queue, condvar, etc. So, which approach do you want? – abarnert Sep 27 '12 at 22:24

1 Answers1

3

A subprocess.PIPE is not just a file-like, it is an actual file. As such you can get its file descriptor and pass it to select.select() or similar functions.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358