By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

subprocess confusion

P: n/a
Hi,
Just discovered that my subprocess call with the preexec_fn wasn't doing
what I thought.
If 'machine' value is different than the current machine name, I want to
remsh the command to that machine, but obviously I misunderstood the
preexec_fn arg.

Should I just put the remsh in the actual command instead of preexec_fn?
thanks,
--Tim Arnold
-------------------------------
if machine == socket.gethostname():
shname = None
else:
shname = lambda :'/bin/remsh %s ' % (machine)
p = subprocess.Popen(preexec_fn = shname,
shell = True,
args = command,
stderr = subprocess.STDOUT,
stdout = log,
env = env,
)
try:
p.wait()
if log:
log.close()
except:
pass

-------------------------------
Apr 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tim Arnold wrote:
If 'machine' value is different than the current machine name, I want to
remsh the command to that machine, but obviously I misunderstood the
preexec_fn arg.
I don't think the return value of preexec_fn is used for anything. You
can use to do things like set process group IDs in the child. For your
use, everything needs to be args.

If you are using Python 2.5, and log is a file, you want:

from __future__ import with_statement

with log:
if machine != socket.gethostname():
command = "/bin/remsh %s %s" % (machine, command)

subprocess.check_call(command, shell=True, env=env,
stderr=subprocess.STDOUT, stdout=log)

The semantics are slightly different, since log will always close this
way, while in the other example, you have left it open if there is an
exception.
--
Michael Hoffman
Apr 16 '07 #2

P: n/a
Tim Arnold <ti****@sas.comwrote:
Just discovered that my subprocess call with the preexec_fn wasn't doing
what I thought.
If 'machine' value is different than the current machine name, I want to
remsh the command to that machine, but obviously I misunderstood the
preexec_fn arg.

Should I just put the remsh in the actual command instead of
preexec_fn?
Yes.

The preexec_fn is run after the fork() but before the exec(). Ie a
new process has been made, but it hasn't started your task yet.

For example a classic use of preexec_fn is

preexec_fn=os.setsid

You seem to be thinking it is pre-pending something to your command
line which isn't how it works.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Apr 17 '07 #3

P: n/a
"Nick Craig-Wood" <ni**@craig-wood.comwrote in message
news:sl*****************@irishsea.home.craig-wood.com...
Tim Arnold <ti****@sas.comwrote:
<snip>
> Should I just put the remsh in the actual command instead of
preexec_fn?

Yes.

The preexec_fn is run after the fork() but before the exec(). Ie a
new process has been made, but it hasn't started your task yet.

For example a classic use of preexec_fn is

preexec_fn=os.setsid

You seem to be thinking it is pre-pending something to your command
line which isn't how it works.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Thanks much to you and Michael H. for the great explanations.
Now everything is working fine, and I understand subprocess a little better!

--Tim Arnold
Apr 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.