Ticket #39 (new idea)

Opened 6 years ago

Last modified 6 years ago

provide raw capabilities for killing and restarting jobs

Reported by: mmckerns Owned by: mmckerns
Priority: major Milestone: pathos-0.4a
Component: pathos Keywords:
Cc:

Description

provide raw capabilities for killing and restarting jobs

Change History

comment:1 Changed 6 years ago by mmckerns

Notes from Nikunj Patel:

I worked on killing processes and all its sub processes after looking into
all possible options we discussed in last meeting. Various options I thought are:
1.        Keeping a list of ‘PID’s’ of parent and all its child somewhere and  during kill
process killing each one in a loop.
2.        Creating a group of processes and sending kill signal to group in such a way
child as well as parent gets kill signal
         if killpg:
                   os.killpg(pgid, signal.SIGKILL)

3.        Waiting for all child process to terminate, for e.g.
    child = subprocess.Popen(‘echo hello world’)
    child.wait()   # wait for the child to terminate

4.        Use popen.poll() command to check if child process has terminated?
       Popen.poll()

Out of all possible options I found following options useful and I have implemented
it and turns out to be successful in most of the cases.
if sys.platform =='win32’:
        import ctypes
        TerminateProcess = ctypes.windll.kernel32.TerminateProcess
        TerminateProcess(int(pipe._handle),-1)
        print "*** Child process terminated !!"
 elif:
        os.kill(pid,signal.SIGKILL)
        killedpid,stat = os.waitpid(pid,os.WNOHANG)
        if killedpid == 0:
            print "*** Child process NOT terminated !!"
       else:
            print "*** Child process terminated !!"

I'm using the "WNOHANG" flag here so that the os.waitpid()  function will return a
tuple of  (0,0) if the process isn't  dead yet. The os.waitpid call attempts to wait
for a child process to exit and returns its process ID and exit status.

For example, some codes for killing all processes both for windows and linux are:

Note: See TracTickets for help on using tickets.