python - Polling a subprocess using pid -
i have django project in allows user start multiple subprocesses @ time. processes may take time , user can continue working other aspects of app. @ instance user can view running processes , poll 1 of them check if over. need poll process using pid. popen.poll() expects subprocess.peopen object while have pid of object. how can 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 want able poll subprocess using p_id e.g like: checkp = pid.poll() #this statement wrong if checkp none: else: else
i need valid statement instead of 'checkp = pid.poll()'
don't poll pid
pid temporary id assigned process. stays process long process running. when process either terminates or killed, pid may(or may not immediately) assigned other process. so, pid of process, valid now, may not remain valid after sometime.
now, how poll process?
this did in 1 of project:
psub = subprocess.popen(something) # polling while psub.poll() none: pass
but beware, above code might result deadlock. avoid deadlock can use 2 approaches:
- use counter. when counter value become greater fixed value - kill process.
2.use timeout. start time when process started , when process takes longer fixed time - kill.
start_time = time.time() timeout = 60 # 60 seconds while psub.poll() none: if time.time() - start_tym >= int(timeout): psub.kill()
Comments
Post a Comment