So far script has used SIGINT and not needed the SIGKILL backup. What would be a better way to terminate a process than SIGINT?
Zombie processes consume minimal resources, but are being reaped eventually anyway so not stacking up. On Fri, Nov 9, 2012 at 9:06 AM, Michele Comitini <michele.comit...@gmail.com > wrote: > using SIGKILL is not a good way to end processes and zombies are evil not > only in "Night of the Living > Dead"<https://en.wikipedia.org/wiki/Night_of_the_Living_Dead> ;-) > they consume resources. > > > mic > > > 2012/11/8 Richard Baron Penman <richar...@gmail.com> > >> thanks for advice, but already have a working solution. >> >> > It's still not clear why the scheduler does not fit your needs. >> I do not need to schedule tasks. I just need to react to form submissions >> so having the scheduler middleman is not necessary in this case. >> >> >> >> On Fri, Nov 9, 2012 at 12:59 AM, Michele Comitini < >> michele.comit...@gmail.com> wrote: >> >>> It's still not clear why the scheduler does not fit your needs. Anyway >>> what you want seems to need should be a *deamon*. >>> You can launch it in the middle of the request/response cycle or from >>> the scheduler it will detach from the parent (the launching) process and >>> work in the background. >>> >>> pip install python-daemon >>> >>> http://pypi.python.org/pypi/python-daemon/ >>> >>> >>> mic >>> >>> >>> 2012/11/7 Richard Baron Penman <richar...@gmail.com> >>> >>>> OK, got a solution that has been working well for last few days now. >>>> >>>> I made 2 mistakes previously that caused me trouble: >>>> >>>> 1) The child processes are independent. I had used ctrl+c to kill >>>> web2py, which was passed on to the child processes. >>>> When kill -9 [web2py PID] was used the child processes continued fine. >>>> >>>> 2) The parent process can kill child processes but they became zombie >>>> processes until the parent process dies. >>>> Originally I was checking /proc/PID for process existence so seemed to >>>> always exist. Now using the psutil package, which has some useful >>>> cross platform features. Much better than parsing output of ps! >>>> >>>> >>>> The scheduler was not helpful for this use case. >>>> >>>> Here are some functions I used in case they help others: >>>> >>>> >>>> def exists(pid): >>>> """Return whether the process exists""" >>>> try: >>>> p = psutil.Process(pid) >>>> if p.status == psutil.STATUS_ZOMBIE: >>>> return False # ignore zombie processes >>>> else: >>>> return True >>>> except psutil.NoSuchProcess: >>>> return False >>>> >>>> >>>> def stop(pid): >>>> """Try to kill this process, first with interrupt and then kill >>>> signal >>>> """ >>>> success = True >>>> try: >>>> p = psutil.Process(pid) >>>> p.terminate() >>>> time.sleep(1) # if don't delay here a bit then exists() call >>>> will usually fail - better way? >>>> if exists(pid): >>>> # was not able to terminate process so try kill >>>> p.kill() >>>> time.sleep(1) >>>> if exists(pid): >>>> success = False >>>> except psutil.NoSuchProcess: >>>> pass >>>> return success >>>> >>>> -- >>>> >>>> >>>> >>>> >>> -- >>> >>> >>> >>> >> >> -- >> >> >> >> > > -- > > > > --