how do i write to a python subprocess' stdin
Question: I'm trying to write a python script that starts a subprocess, and writes to
the subprocess stdin. I'd also like to be able to determine an action to be
taken if the subprocess crashes.

The process I'm trying to start is a program called nuke which has its own
built in version of python which I'd like to be able to submit commands to,
and then tell it to quit after the commands execute. So far I've worked out
that if start python in the command prompt like and then start nuke as a
subprocess then i can type in commands to nuke but id like to be able to put
this all in a script so that the master python program can start nuke and then
write to its stdin (and thus into its built in version of python) and tell it
to do snazzy things, so i write a script that start nuke like this:

    
    
    subprocess.call(["C:/Program Files/Nuke6.3v5/Nuke6.3", "-t", "E:/NukeTest/test.nk"])
    

Then nothing happens because nuke is waiting for user input, does anyone know
how I'd now write to the stdin?

I'm doing this because I'm running a plugin with nuke that causes it to crash
intermittently when rendering multiple frames. So I'd like this script to be
able to start nuke, tell it to do something and then if it crashes try again.
So if there is a way to catch a crash and still be ok then that'd be great.


Answer: It might be better to use
[`communicate`](http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate):

    
    
    from subprocess import Popen, PIPE, STDOUT
    p = Popen(['myapp'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
    stdout_data = p.communicate(input='data_to_write')[0]
    

"Better", because of this warning:

> Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to
> avoid deadlocks due to any of the other OS pipe buffers filling up and
> blocking the child process.

