Search code examples
pythonwindowsprocessterminate

Python: calling process.terminate() doesn't kill it


When I run the following code:

p = subprocess.Popen("...", shell=True)          
if p.poll() == None:
  p.kill()

The process is simply not killed. I'm on Windows.

I thought it was because of multithreading, I ran it in a single thread, still same thing.

Do you have any ideas why this could be happening?

Thanks

Update

I found the code that causes the problem:

while cur_time < self.time_limit:
            if p.poll() != None:
                too_much_time = False
                break
            time.sleep(0.1)
            cur_time += 0.1

I run this to make sure that the process doesn't take more than the time limit. Apparently that's why I can't kill the process.


Solution

  • Because you use shell=True, all you end up killing is the shell (cmd.exe process) itself.

    Process groups are a relatively new feature of Windows and I don't know of any software apart from Task Scheduler that actually uses them yet.