Code_aholic Code_aholic - 1 year ago 109
Python Question

Polling a subprocess using pid

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?


class Runningprocess(models.Model):
#some other fields
def __str__(self):
return self.field1


def run_process():
p= subprocess.Popen(cmd, shell =True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return HttpResponseRedirect('/home/')

def check(request,pid):
#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
do something else

I just need a valid statement instead of 'checkp = pid.poll()'

Answer Source

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:

but beware, the above code might result into a deadlock. To avoid deadlock you can use two approaches:

  1. Use counter. When counter value become greater than some fixed value - kill the process.

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):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download