0

I have a gui in which it should open a program and the quit the program when the quit button is pressed. The code is as follows:

#!/usr/bin/python
import Tkinter as tk

import subprocess
import os
import signal

class StageGui:
    def __init__(self,parent):
        self.process = None
        self.f = tk.Frame(main, width=300, height=300)
        self.b1=tk.Button(main,text='Start Medina',command=self.startmedina).pack(side='left',anchor='nw')
        self.b2=tk.Button(main,text='Quit Medina',command=self.quitmedina).pack(side='left',anchor='nw')
        self.xf = tk.Frame(self.f,relief='groove', borderwidth=2)

    def startmedina(self):
        self.process=subprocess.Popen(['pre xx'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,preexec_fn=os.setsid)
        return
    def quitmedina(self):
        os.killpg(self.process.pid,signal.SIGKILL)
        return  
main = tk.Tk()
stagegui=StageGui(main)
main.title('prototype')

main.mainloop()

When i press the start button the program opens but when press the quit it does'nt kill the subprocess. can any one help me with this?

ayaan
  • 715
  • 5
  • 18
  • 36
  • Looks like it should work, probably - I don't know what medina is, is it a server? What happens if you start medina manually from the command line and then try to kill it using the `kill` command? It could be that medina traps SIGTERM and that it doesn't terminate the process. Try it manually. – mhawke Aug 04 '14 at 04:58
  • Also, unrelated to the problem, you might have a typo `prcoess=0` which I assume should be `process = 0`. – mhawke Aug 04 '14 at 04:59
  • medina is a preprocessor application which is used in modelling and analysis of cars.. – ayaan Aug 04 '14 at 05:14
  • Try using `kill` instead of `terminate`. They are synonyms under Windows, but on various Unixes, `terminate` sends a trappable `SIG_TERM` signal whereas `kill` send (more brutal) non trappable `SIG_KILL`. – Serge Ballesta Aug 04 '14 at 06:04
  • Do you really need [`shell=True`](http://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess)? It should be avoided. – OrangeTux Aug 04 '14 at 07:33
  • @ayaan : what happened when you tried to kill medina from the command line? – mhawke Aug 04 '14 at 07:34

1 Answers1

0

Use StageGui.process.terminate() to kill the process, rather than os.kill. If that doesn't work, you can try StageGui.process.kill(), though that is the equivalent of sending a SIGKILL (assuming you're on a POSIX system), which is a non-graceful way to shutdown a process.

Also, do you really want process to be a class attribute, rather than an instance attribute? It might make more sense to set self.process = None in __init__, and self.process = Popen(... in startmedina.

dano
  • 91,354
  • 19
  • 222
  • 219
  • eventhough i use self.process.terminate() the program does'nt close @dano – ayaan Aug 04 '14 at 05:19
  • On *nix `subprocess.terminate()` is the same as `os.kill(pid, SIGTERM)`, so you wouldn't expect any difference in behaviour. `subprocess.kill()` should work, but it gives the terminated process no chance to clean itself up before exit, so you really don't want to be doing that. – mhawke Aug 04 '14 at 07:32
  • i modified the program a bit... i used the try and except blocks and figured out what is the exception. i modfied the line subprocess.Popen(.... shell=TRUE, preexec_fn=os.setsid). you can see the modified code above @mhawke – ayaan Aug 04 '14 at 08:42