[Tutor] subprocess Popen

Tiago Saboga tiagosaboga at gmail.com
Sat Apr 4 02:25:11 CEST 2009


On Fri, Apr 03, 2009 at 03:12:08PM -0700, Weidner, Ronald wrote:
> I have a long running py script that I'm trying to kick off from
> another long running py script as a separate process... If either
> script fails or terminates, I don't want the other script to be
> effected as a result. In other words I need a separate process not a
> new thread. In any case, this code I thought would work but it
> doesn't.
> 

> someLongRunningScript= ( "%s/someLongRunningScript.py" % ( os.getcwd() ) )
> someLongRunningScriptArgs= ( '--verbose --directory=%s --link=%s %s' % ( directory_name, link_name, span_option ) )
> longCommand = ( '%s %s' % ( someLongRunningScript, someLongRunningScriptArgs) )
> pid = subprocess.Popen ( ["/usr/bin/python", longCommand ] ).pid
> print ( "/usr/bin/python %s " % ( longCommand ) )

> 
> What's interesting is that if I copy the text printed in the 5th
> line, paste into my shell, then run it -- the process I'm trying to
> start works perfectly.
> 
> The problem seems to have something to do with the way arguments are
> being passed to the python script named someLongRunningProcess.py.

Yes, the arguments are not passed to the shell. There is a Popen
option to pass them to a shell, perhaps it would work. But the simpler
way is to write:

pid = subprocess.Popen(["/usr/bin/python"] + longCommand.split()).pid

It will not work if some arguments are quoted strings with spaces. It
would be better if longCommand was already a list.

Tiago.


More information about the Tutor mailing list