Python: Kill (terminate) a process from command prompt using Python
Question: I am not very familiar with the computer software terminology (my apologies).

  * I designed a GUI in Python that suppose to execute and/or terminate a script when an appropriate button (on the GUI) is pressed.
  * When I press a "Start" button (QPushButton) on the GUI, the following command executes the script _filename.dut_ in the command prompt as follows:  
subprocess.call ('launcher.exe localhost filename.dut', shell=True).

  * I want to be able to terminate the script in a similar way, i.e., to press a "Stop" button on the GUI that will write into the command prompt an appropriate command to terminate the script. I think I can find the solution in this thread: [Ending external programs with Python](http://stackoverflow.com/questions/6026791/ending-external-programs-with-python). The suggested solution is:
    
        import subprocess        
    taskname = '...'        
    task = 'taskkill /im ' + taskname + ' /f'         
    subprocess.check_call(task, shell=True)
    

  * My quastion is how can I obtain the _taskname_ ?

Any suggestions or alternate solution would be very appreciated.


Answer: If you use `subprocess`, you don't need to call any external utilities.
`subprocess.Popen` class provides `terminate` method.

In order to use it, you'll need to replace `subprocess.call(...)` with
`subprocess.Popen(...)`, which returns a `Popen` instance. For example,

    
    
    task = subprocess.Popen('launcher.exe localhost filename.dut', shell=True)
    # some time later
    task.terminate()
    

Note that, unlike `call`, plain `Popen` doesn't wait for process to complete.

Consult the manual for more details:
<http://docs.python.org/2/library/subprocess.html>

