How to kill this python process opened using subprocess.pOpen() in Windows?

  kill-process, python, python-3.x, subprocess, windows

The code below to kill a process opened using subprocess.pOpen() works on Linux.

import os
import signal
import subprocess

# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before  exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                       shell=True, preexec_fn=os.setsid) 

os.killpg(os.getpgid(pro.pid), signal.SIGTERM)  # Send the signal to all the process groups

Unfortunately, it does not work on Windows. The process refused to be killed. How to modify the code such that the process can be killed on Windows?

I am using Windows 10, python 3.8 on anaconda.

Source: Windows Questions

LEAVE A COMMENT