Killing a Python subprocess

I have problems with subprocesses. The following code fragment is used to create a subprocess -

  while(not myQueue.empty()): 
        p=Popen(myQueue.get(),shell=True,stdin=PIPE,stderr=PIPE)

I create processes iterating until the queue (which has the commands in it) is empty. The variable p is global and is an object of type Popen. Despite the fact that the team did what it was supposed to do, I am having problems with the stop button, which does not stop the process, as I expected.

The stop button code is as follows:

  stop=Button(textBoxFrame,text="Stop",width=5,command=stopAll)
  stop.grid(row=1,column=4)

The stopAll method is called by the aforementioned stop button, which will kill the current subprocess p.

  def stopAll():
        p.kill()

NOTE. No errors, exceptions or compilation issues.

UPDATE: , p.kill() , . unix, >> ps aux. PID, ps aux. , , , 6 PIDs p.pid, os.kill((p.pid)+6,signal.SIGKILL), . , , . .

, , , . :

    echo "Hello"|festival --tts

- unix, festival --tts . "Hello" , . p, , echo, festival. , , () .

+5
4

UNIX, preexec_fn. , , , , . , :

   while(not myQueue.empty()): 
    p=Popen(myQueue.get(),shell=True, stdin=PIPE,preexec_fn=os.setsid)

stopAll , p -

    os.killpg(os.getpgid(p.pid), signal.SIGKILL)

, . :

0

, , stopPressed stopAll() , ,

>>> fred = '20'
>>> def updateFred(age):
...     fred=age
>>> updateFred(40)
>>> fred
'20'
>>> def updateFred(age):
...     global fred
...     fred=age
>>> updateFred(40)
>>> fred
40

, "global stopPressed" stopAll() ?

+1

, . , ( ). , stopprcs ? ( ?).

0
source

As @aid mentioned, if you do not explain, make the handle global, you cannot change it for everyone else. Try this instead of the stopAll function

def stopAll():
    global stopPressed
    stopPressed=True

I would suggest, instead of messing with the globals, and what not to create a class, for example

class GuiController(object):
    # if you want the stopPressed to be a static variable accross all
    # GuiController instances uncomment the next line and comment out the __init__
    #stopPressed = False
    def __init__(self):
        self.stopPressed=False
    def main(self):
        while(not myQueue.empty()): 
            p=Popen(myQueue.get(),shell=True,stdin=PIPE,stderr=PIPE)
            while(p.returncode==None):
                if(stopPressed==True):
                    p.kill()
                    break
        self.stopPressed=False
    def stopAll(self):
        self.stopPressed=True
0
source

All Articles