Is there a way to check if the subprocess is still running?

I am running several subprocesses with subprocess.Popen in Python. I would like to check if one such process is complete. I found two ways to check the status of the subprocess, but both of them seem to force the process to shut down. One uses process.communicate () and prints the return code as described here . The other just calls process.wait () and checks that it returns 0.

Is there a way to check if the process is running without waiting for it to complete?

+8
python subprocess
source share
3 answers

Ouestion : ... a way to check if the process continues ...

You can do this, for example:

p = subprocess.Popen(... """ A None value indicates that the process hasn't terminated yet. """ poll = p.poll() if poll == None: # p.subprocess is alive 

Python "3.6.1 Documentation Object Panels

Tested with Python: 3.4.2

+8
source share

You can use subprocess.check_output to see your result.

Try this code:

 import subprocess subprocess.check_output(['your command here'], shell=True, stderr=subprocess.STDOUT) 

Hope this helps!

0
source share

Performance

 myProcessIsRunning = poll() is None 

As suggested by the main answer, this is the recommended method and the easiest way to check if the process is working. (and it works in jython too)

If you do not have an instance of the process, check it out. Then use the TaskList / Ps operating processes.

In windows, my command will look like this:

 filterByPid = "PID eq %s" % pid pidStr = str(pid) commandArguments = ['cmd', '/c', "tasklist", "/FI", filterByPid, "|", "findstr", pidStr ] 

This essentially does the same as the following command line:

 cmd /c "tasklist /FI "PID eq 55588" | findstr 55588" 

And on linux, I do the same with:

 pidStr = str(pid) commandArguments = ['ps', '-p', pidStr ] 

The ps command already returns error code 0/1 depending on whether the process is found. In windows you need the find string command.

This is the same approach that is discussed in the flow of the following elements:

Check if a process is running using PID in JAVA

Note: If you use this approach, remember to wrap your command call in
try: foundRunningProcess = subprocess.check_output (argumentsArray, ** kwargs) return True except Exception as err: return False

Please note, be careful if you are developing VS code and using pure Python and Jython. In my environment, I was under the illusion that the poll () method did not work, because the process, which I suspected was supposed to end, was indeed running. This process launched Wildfly. And after I asked the wildfly to stop, the shell was still expecting the user to "Press any key to continue ...".

To complete this process, the following code worked on pure python:

 process.stdin.write(os.linesep) 

In jython, I had to fix this code to look like this:

 print >>process.stdin, os.linesep 

And with this difference, the process really ended. And jython.poll () started telling me that the process is indeed complete.

0
source share

All Articles