Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm launching a number of subprocesses with subprocess.Popen in Python. I'd like to check whether one such process has completed. I've found two ways of checking the status of a subprocess, but both seem to force the process to complete. One is using process.communicate() and printing the returncode, as explained here. Another is simply calling process.wait() and checking that it returns 0.

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

like image 255
bjarkemoensted Avatar asked Apr 07 '17 09:04

bjarkemoensted


People also ask

How do you wait for a subprocess to finish?

A Popen object has a . wait() method exactly defined for this: to wait for the completion of a given subprocess (and, besides, for retuning its exit status). If you use this method, you'll prevent that the process zombies are lying around for too long. (Alternatively, you can use subprocess.

How do you close a subprocess in Python?

To close a single subprocess in Python, use the kill() method. The kill() is a built-in method used for terminating a single subprocess. The kill() command keeps running in the background.

Does subprocess run wait for finish?

subprocess. run() is synchronous which means that the system will wait till it finishes before moving on to the next command.


2 Answers

Ouestion: ... a way to check if a process is still running ...

You can do it for instance:

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

Python » 3.6.1 Documentation popen-objects

Tested with Python:3.4.2

like image 182
stovfl Avatar answered Sep 25 '22 02:09

stovfl


Doing the

myProcessIsRunning = poll() is None  

As suggested by the main answer, is the recommended way and the simplest way to check if a process running. (and it works in jython as well)

If you do not have the process instance in hand to check it. Then use the operating system TaskList / Ps processes.

On windows, my command will look as follows:

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

This is essentially doing the same thing as the following command line:

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

And on linux, I do exactly the same using the:

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

The ps command will already be returning error code 0 / 1 depending on whether the process is found. While on windows you need the find string command.

This is the same approach that is discussed on the following stack overflow thread:

Verify if a process is running using its PID in JAVA

NOTE: If you use this approach, remember to wrap your command call in a try/except:

try:     foundRunningProcess = subprocess.check_output(argumentsArray, **kwargs)     return True except Exception as err:     return False 

Note, be careful if you are developing with VS Code and using pure Python and Jython. On my environment, I was under the illusion that the poll() method did not work because a process that I suspected that must have ended was indeed running. This process had launched Wildfly. And after I had asked for wildfly to stop, the shell was still waiting for user to "Press any key to continue . . .".

In order to finish off this process, in pure python the following code was working:

process.stdin.write(os.linesep) 

On jython, I had to fix this code to look as follows:

print >>process.stdin, os.linesep 

And with this difference the process did indeed finish. And the jython.poll() started telling me that the process is indeed finished.

like image 45
99Sono Avatar answered Sep 24 '22 02:09

99Sono