Search code examples
python-3.xpopenpid

Get pid during long process with subprocess popen


I need the PID from a process I run with Popen in Python 3.5.2.

Having this:

with open(info['stdout_file'], 'w') as logfile:
    prcs = sp.Popen(split, 
                    stdout=logfile, 
                    stderr=logfile, 
                    cwd=info['runfolder'])

streams = prcs.communicate()
out, err = streams

post = {'pid': prcs.pid}
r = requests.post('https://.../receive_status.php', data=post)

the PID of the process gets posted only after the process is finished. But I need it during the process to be able to kill is. Is there any way? Because killing the


Solution

  • The pid is available as soon as subprocess.Popen returns the objects, no need to wait. Is the communicate function that wait the process to finish.