You are not logged in.

#1 2009-12-26 16:51:20

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Python - Spawn Program

Hello.
I have a webpy app in which I want to execute cvlc from.

Currently the code I am using is this:

pid = subprocess.Popen(['/usr/bin/cvlc', 'pvr:///dev/video0', '--sout', '#rtp{dst=239.255.1.1,port=5004,mux=ts}'],stdout=subprocess.PIPE,stderr=subprocess.PIPE).pid

However, whenever I kill the process it stays defuncted until I kill the python script.

What can I do to prevent this from happening?

I am guessing I have to spawn the program into its own parent process.


Website - Blog - arch-home
Arch User since March 2005

Offline

#2 2009-12-26 18:07:09

sctincman
Member
From: CO (USA)
Registered: 2009-04-08
Posts: 85

Re: Python - Spawn Program

Not sure if there is anything in python, but you could try executing it with  the "setsid" command. That should launch that program in a news separate session from python

Offline

#3 2009-12-26 19:17:55

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: Python - Spawn Program

I've never noticed this behaviour in my apps you can try to use pid.poll to see if the subprocess has terminated, if it is reporting a return code but still be listed as a process try doing delete pid to see if it cleans it up.

Offline

#4 2009-12-26 20:15:07

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: Python - Spawn Program

Well I'm killing it with os.kill(), the thing is, streaming might go on for hours and I need to be able to serve my webapp while the streaming is taking place.


Website - Blog - arch-home
Arch User since March 2005

Offline

#5 2009-12-27 00:56:22

csstaub
Member
From: Switzerland
Registered: 2009-02-09
Posts: 37

Re: Python - Spawn Program

I think your script needs a signal handler for SIGCHLD (see http://docs.python.org/library/signal.html).

Offline

#6 2009-12-27 04:04:21

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: Python - Spawn Program

Have you tried running the program by itself (not with python) using the same parameters? Does it close die properly?

Just wrote a quick little program to test this...

import subprocess
import time

subprc = subprocess.Popen("gnome-mplayer",stdout=subprocess.PIPE,stderr=subprocess.PIPE)

print "PID: {0}".format(subprc.pid)

while (subprc.poll() == None):
    print(subprc.returncode)
    time.sleep(1)

and it works fine, process dies when app closes.
So my best guess is that cvlc isn't closing properly

Offline

#7 2009-12-27 15:39:22

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: Python - Spawn Program

But see that is the problem... I can't wait for the app to close. I need the app to fork/spawn/whatever in the background so my webapp can continue to work (and not wait for the program to close). It seems like the best way to do this is with a bash helper script in which I use & to fork the program in the background.


Website - Blog - arch-home
Arch User since March 2005

Offline

#8 2009-12-27 16:00:18

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: Python - Spawn Program

subprocess does fork the application to the background, all my test code does is check to make sure the process is closing by itself.
why not try my little test code with cvlc and a short video and see if cvlc closes by itself, if it isn't thats your problem. All I'm saying is that
subprocess works the way you want it to, the problem lies elsewhere.

Offline

#9 2009-12-27 16:13:57

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: Python - Spawn Program

Not really...

import subprocess
import time

subprc = subprocess.Popen("gedit",stdout=subprocess.PIPE,stderr=subprocess.PIPE)

print "PID: {0}".format(subprc.pid)


time.sleep(60)

Now if you run killall gedit and ps aux in another terminal you'll see

pyther   17315  6.2  0.0      0     0 pts/4    Z+   11:09   0:00 [gedit] <defunct>

before the script exits. In my case the script will never exit, because it is a webpy app so over a month or so, I'd have at least 30 zombie processes.

However, I ended up finding a hidden switch for vlc --daemon which works wonders.


Website - Blog - arch-home
Arch User since March 2005

Offline

#10 2009-12-27 16:40:02

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: Python - Spawn Program

Sorry, I should have caught that, poll cleans up the process for you.

I know you solved this by other means but another solution would be to have a garbage collection array something like this...

OldProcs = []

def CleanUp():
    while i < len(OldProcs):
         if (OldProcs[i].poll() != None):
             OldProcs.pop(i)
         else:
             i++

Then just add a couple of lines to your code

global OldProcs

OldProcs.append(subprc)
CleanUp()

Not ideal but effective none the less

Offline

#11 2009-12-27 16:51:14

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Python - Spawn Program

sctincman wrote:

Not sure if there is anything in python, but you could try executing it with  the "setsid" command. That should launch that program in a news separate session from python

This is what I do too, with an extra setsid (I found that works better) and signal.SIGCHLD left at the default:
Popen("setsid setsid "+cmd+" >/dev/null 2>&1 < /dev/null", shell=True)

Offline

#12 2009-12-27 20:17:19

lman
Member
From: CZ
Registered: 2007-12-18
Posts: 255

Re: Python - Spawn Program

I think this will help:
http://code.activestate.com/recipes/278731/

you can use the createDaemon function to spawn a new process. It just need a little editing.

(I used it once to start the xmms2 daemon, if it's any help: http://code.google.com/p/etude-music-pl … ain.py#379)

Offline

Board footer

Powered by FluxBB