Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polling a subprocess using pid [closed]

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?

in models.py:

class Runningprocess(models.Model):
    #some other fields
    p_id=models.CharField(max_length=500)
    def __str__(self):
        return self.field1

In views.py:

def run_process():
    ....
    p= subprocess.Popen(cmd, shell =True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    new_p=Runningprocess()  
    new_p.p_id=p.pid
    ....
    new_p.save()
    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
     else:
           do something else

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

like image 282
Code_aholic Avatar asked Mar 02 '26 00:03

Code_aholic


1 Answers

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:

  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):
        psub.kill()
like image 167
Amit Tripathi Avatar answered Mar 03 '26 14:03

Amit Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!