Search code examples
pythonpopen

How to attach to running Popen process


I have a shell script that runs for a while and then takes an input. I want to create the process:

process = subprocess.Popen( 
     '/my/longscript/wait.sh', 
     shell=True, 
     stdout=subprocess.PIPE, 
     stdin=subprocess.PIPE, 
     stderr=subprocess.PIPE, 
     universal_newlines=True 
 )                                                                                                                                                                                   

process                                                                                                                                                                             
<subprocess.Popen at 0x7fb7b319ef60>

process.pid                                                                                                                                                                         
10248

And then in a different session attach to this process and send it some stdin. How can I reattach to the process, using the pid, or how do I do this?

running_process = subprocess.attach_to_my_old_process(pid=10248)???
running_process.communicate(input='someinput')

Solution

  • Turning my comment into an answer: you have to use a named pipe for this.

    How this can be done was done I answered in your related question.