1

In python I have opened 4 subprocess. Now I want to kill all previous process when new request is came in python script.

I am using python 2.7 and windows 7 OS.

Thanks,

CrazyCoder
  • 772
  • 5
  • 11
  • 31
  • 1
    How are you spawning the 4 subprocesses and what have you tried so far? – gaige May 09 '13 at 09:43
  • Actually my aim is when new request come for process, need to stop previous process. – CrazyCoder May 09 '13 at 09:47
  • What is dispatching the subprocesses? If you want to kill the previous processes, the originator (owner) of those processes will have the information necessary to kill them. – gaige May 09 '13 at 09:48
  • for e.g. I have two process, p = subprocess.Popen("echo t |", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p1 = subprocess.Popen([svn, "list", "-R", Url], shell=True, stdin=p.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() , Now I need to terminate these two process, when new request came. – CrazyCoder May 09 '13 at 09:56
  • Please put that code formatted into your question. It is really hard to read in the comment and important information. – Thomas Fenzl May 09 '13 at 10:56
  • just call [`p.terminate()`](http://docs.python.org/2/library/subprocess.html#subprocess.Popen.terminate) – jfs May 11 '13 at 00:35

3 Answers3

4

Assuming you want to kill all children processes without keeping track of them, the external lib psutil makes this easy:

   import os
   import psutil
   # spawn some child processes we can kill later
   for i in xrange(4): psutil.Popen('sleep 60')

   # now kill them
   me = psutil.Process(os.getpid())
   for child in me.get_children():
       child.kill()
itai
  • 1,566
  • 1
  • 12
  • 25
  • When I have used this code, I am getting the eror: WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'abc.txt' – CrazyCoder May 09 '13 at 10:25
  • Thats very odd... Few options that come to mind - 1. you dont have sleep.exe on your system (though that wouldn't be error 32), 2. maybe its somehow affected by the rest of your code. have you tried it just in the interpreter? – itai May 12 '13 at 07:16
1

In your main python script where you are spawning subprocess send/pass an Event object with it and keep reference of your subprocess with event in main process

Sample Code:

from multiprocessing import Process, Event

# sub process execution point
def process_function(event):
    # if event is set by main process then this process exits from the loop
    while not event.is_set():
        # do something

# main process

process_event = {}  #  to keep reference of subprocess and their events
event = Event()
p = Process(target=process_function, args=(event))
p.start()
process_event[p] = event

# when you want to kill all subprocess
for process in process_event:
    event = process_event[process]
    event.set()

Edit
As you commented to your question, I think its not quite useful in your scenario as you are using subprocess.Popen.But a nice trick though

Rahul Gautam
  • 4,749
  • 2
  • 21
  • 30
-1

You can use os.kill function

import os
os.kill(process.pid)

If you open the subprocess using the subprocess.Popen function already returns the process id. But be careful if you are using the shell=True flag, because in that case the process pid will be the shell process id. If this is your case here are a posible solution.

Community
  • 1
  • 1
jvallver
  • 2,230
  • 2
  • 11
  • 20