2

This question is in relation to:

python, subprocess: reading output from subprocess

If P is a subprocess started with a command along the lines of

import subprocess

P = subprocess.Popen ("command", stdout=subprocess.PIPE)

we can read the output P produces by P.stdout.readline (). This is a blocking read though. How can I check if there is output ready for reading (without blocking)?

2

1 Answer 1

0

If you are using *nix, then you can use the select module to poll the stdout file descriptor

import subprocess
import select
poller = select.epoll()

P = subprocess.Popen ("command", stdout=subprocess.PIPE)
poller.register(P.stdout, select.EPOLLHUP)

while True:
    #block indefinitely: timeout = -1
    #return immediately: timeout = 0
    for fd, flags in poller.poll(timeout=0)
        foo = P.stdout.readline()
    #do something else before the next poll
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.