1

If I run the following python code (in python 2.7) I get an empty output file while I would expect a line. What is wrong?

import subprocess

with open('outfile.out', 'w') as out:
   pp=subprocess.Popen(['/bin/cat'],stdin=subprocess.PIPE,stdout=out)
   pp.stdin.write('Line I want into out file\n')
   pp.terminate()

1 Answer 1

2

You terminated the process, and never flushed/closed the input to it, so all the data was stuck in buffers and discarded when the process was forcibly killed. You can use communicate to combine passing input, closing stdin, then waiting for the process to complete:

import subprocess

with open('outfile.out', 'w') as out:
   pp=subprocess.Popen(['/bin/cat'],stdin=subprocess.PIPE,stdout=out)
   pp.communicate('Line I want into out file\n')

In this case (only one of the three standard handles is a pipe), you could also do this safely:

import subprocess

with open('outfile.out', 'w') as out:
   pp=subprocess.Popen(['/bin/cat'],stdin=subprocess.PIPE,stdout=out)
   pp.stdin.write('Line I want into out file\n')
   pp.stdin.close()
   pp.wait()  # Optionally with a timeout, calling terminate if it doesn't join quickly

That should only be done if you are only using a single standard handle as PIPE; if more than one is PIPE, there is a risk of deadlock (child is writing to stdout, waiting for you to read to clear the buffer, you are writing to stdin, waiting for child to read to clear the buffer) that communicate resolves by using threads or the select module, and you'd have to mimic that design to avoid deadlock.

Sign up to request clarification or add additional context in comments.

2 Comments

use pp.wait() instead of pp.join() (it is subprocess, not multiprocessing). There is no timeout parameter in Python 2.7 (unless you use subprocess32 module).
@J.F.Sebastian: Fixed the wait vs. join bit; your comment can clarify when timeout is available.

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.