Subprocess or Process or OMG!!

R

rh0dium

Hi all,

Here I was happily coming to working thinking - OK I need to create a
wrapper for a tool (UNIX) which does nothing but lauch the end tool and
send a sql instert letting the db know the tool was launched (Can we
say Big Brother..). Some of the tools are very long running with lots
of data others are small and very fast.. So I started down my
traditional approach - Check google groups for the answer. I quickly
became aware of how challenging this actually is. See my approach was
simple

Tool name = FOOBAR
Symbolically link FOOBAR to my Wrapper.py

1. Get the args and some other key data (user, cwd, etc)
2. Fork this process and get FOOBAR running (time is of essense of
course)
3. On the side submit a mySQL call saying what happened...

Sounds simple and I thought I was ready to go.. Until google groups...

http://groups.google.com/group/comp...q=Python+Fork+Wrapper&rnum=3#79e4b404b4f0e1ff

OK so now I need to think about I/O redirection hmmm OK More data..
People have dealt with this - but most of it's dated..

Wait import subprocess.. Looks promising hey some has really used
this..

http://www.dalkescientific.com/writings/diary/archive/2005/04/12/wrapping_command_line_programs.html
http://www.third-bit.com/swc2/lec/integrate.html
http://www.easysw.com/~mike/cups/strfiles/1648/smtp

Needless to say this is much more complex than what I was "hoping" for.
Has anyone solved this in a generic approach..

#!/usr/bin/env python

import os,sys,string
import subprocess

argc = len(sys.argv)

# Yes I move all exe's to the .bin direcotory
sys.argv[0]=os.path.join(os.getcwd() + "/.bin/" +
os.path.basename(sys.argv[0]))

cmd = string.join(sys.argv," ")
subprocess.Popen( cmd )

This has several problems - least of which args aren't working.... Has
anyone really tried this approach?
 
R

Robert Kern

rh0dium said:
Needless to say this is much more complex than what I was "hoping" for.
Has anyone solved this in a generic approach..

#!/usr/bin/env python

import os,sys,string
import subprocess

argc = len(sys.argv)

# Yes I move all exe's to the .bin direcotory
sys.argv[0]=os.path.join(os.getcwd() + "/.bin/" +
os.path.basename(sys.argv[0]))

cmd = string.join(sys.argv," ")
subprocess.Popen( cmd )

This has several problems - least of which args aren't working.... Has
anyone really tried this approach?

Did you read the documentation? It explains how to pass the command line (hint:
it's a list, not a string).

http://docs.python.org/lib/module-subprocess.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
B

blair.bethwaite

rh0dium said:
This has several problems - least of which args aren't working.... Has
anyone really tried this approach?

No, they just wrote the code for the hell of it. :)

Seriously though, you may want to consider using the popen2 module.
Then you'll be able to wait on the subprocess to return and check the
return code, plus you could do your DB update first then go into a
wait() to check for success.

-B
 
R

rh0dium

ROTFLMAO - Thanks --- I "meant" has anyone done the wrapper approach
before!! Not has anyone used the code.. hahah nice!!

subprocess also has the wait object. I think I like this better than
popen2 (which I have used in the past) for a number of reasons but
primarily it's simplicity. Having said that, I don't think I want to
use the wait object because I am afraid the db query in some cases may
take longer than the command.. That would leave the user wondering
what's going on.. Only testing will tell though..

Thanks Robert - yeah I checked the docs and figured out that it takes a
list shortly after I sent the post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top