Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess killing

I have problem killing sub processes. The following piece of code is used for creating the sub process-

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

I'm creating processes by iterating until the queue(which has commands in it) is empty. The variable p is global and is an object of type Popen. Even though the command has done what it is supposed to do, I'm having problems with stop button which is not stopping 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 above stop button which will kill the current subprocess p.

  def stopAll():
        p.kill()

NOTE-There are no errors, Exceptions or any compiling problems.

UPDATE: The problem is that p.kill() was not killing the process I needed to kill. I checked this with unix using >> ps aux. I have also made my program to output starting and killing PIDs, so that I can check them with ps aux. I found the process that I need to kill was 6 PIDs away from p.pid, I tried killing the process like os.kill((p.pid)+6,signal.SIGKILL) which is working and stopping the correct process. But I don't want to do in that way as there are chances that may cause different child processes getting killed. I'm giving more details on my problem-

The Queue I'm using here contains commands as I said earlier. The command is something like this-

    echo "Hello"|festival --tts

Festival is a speech synthesizer in unix and festival --tts gets user input from a file. I'm piping "Hello" to festival and it says the words correctly. But p which is the process of performing the above command is killing echo and not festival. So please help me with killing the particular (festival) process.

like image 954
Alphaceph Avatar asked Nov 13 '22 18:11

Alphaceph


1 Answers

I suspect you may be having issues as you're not declaring stopPressed inside your stopAll() function as global, by example,

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

Perhaps adding 'global stopPressed' at the start of stopAll() will help?

like image 154
aid Avatar answered Dec 16 '22 07:12

aid