I have a django project in which allows a user to start multiple subprocesses at a time. The processes may take some time and thus the user can continue working with other aspects of the app. AT any instance the user can view all the running processes and poll any one of them to check if its over. For this I need to poll the process using its pid. Popen.poll() expects a subprocess.Peopen object while all I have is the pid of that object. How can I get the desired result?
#some other fields
p= subprocess.Popen(cmd, shell =True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#here i want to be able to poll the subprocess using the p_id e.g something like:
checkp = pid.poll() #this statement is wrong
if checkp is None:
do something else
Don't poll with PID
PID is a temporary ID assigned to a process. It stays with a process as long as process is running. When a process either terminates or get killed, its PID may(or may not immediately) get assigned to some other process. So, a PID of a process, which is valid now, may not remain valid after sometime.
Now, how to poll for a process?
This is what I did in one of my project:
psub = subprocess.Popen(something) # polling while psub.poll() is None: pass
but beware, the above code might result into a deadlock. To avoid deadlock you can use two approaches:
2.Use timeout. Get the start time when process started and when process takes longer than a fixed time - kill.
start_time = time.time() timeout = 60 # 60 seconds while psub.poll() is None: if time.time() - start_tym >= int(timeout): psub.kill()