Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Make the pipes non-blocking by using <a href="http://docs.python.org/library/fcntl.html#fcntl.fcntl" rel="noreferrer"><code>fcntl.fcntl</code></a>, and use <a href="http://docs.python.org/library/select.html#select.select" rel="noreferrer"><code>select.select</code></a> to wait for data to become available in either pipe. For example:</p> <pre><code># Helper function to add the O_NONBLOCK flag to a file descriptor def make_async(fd): fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) # Helper function to read some data from a file descriptor, ignoring EAGAIN errors def read_async(fd): try: return fd.read() except IOError, e: if e.errno != errno.EAGAIN: raise e else: return '' process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) make_async(process.stdout) make_async(process.stderr) stdout = str() stderr = str() returnCode = None while True: # Wait for data to become available select.select([process.stdout, process.stderr], [], []) # Try reading some data from each stdoutPiece = read_async(process.stdout) stderrPiece = read_async(process.stderr) if stdoutPiece: print stdoutPiece, if stderrPiece: print stderrPiece, stdout += stdoutPiece stderr += stderrPiece returnCode = process.poll() if returnCode != None: return (returnCode, stdout, stderr) </code></pre> <p>Note that <code>fcntl</code> is only available on Unix-like platforms, including Cygwin.</p> <p>If you need it to work on Windows without Cygwin, it's doable, but it's much, much tougher. You'll have to:</p> <ul> <li>Use the <a href="http://sourceforge.net/projects/pywin32/" rel="noreferrer">pywin32</a> library to call through to the native Win32 API</li> <li>Use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365787%28v=VS.85%29.aspx" rel="noreferrer"><code>SetNamedPipeHandleState</code></a> with <code>PIPE_NOWAIT</code> to make the stdout and stderr pipes non-blocking</li> <li>Use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025%28v=vs.85%29.aspx" rel="noreferrer"><code>WaitForMultipleObjects</code></a> instead of <code>select</code> to wait for data to become available</li> <li>Use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx" rel="noreferrer"><code>ReadFile</code></a> to read the data</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload