subprocess confusion

T

Tim Arnold

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

-------------------------------
 
M

Michael Hoffman

Tim said:
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.
 
N

Nick Craig-Wood

Tim Arnold said:
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.
 
T

Tim Arnold

Nick Craig-Wood said:
Tim Arnold <[email protected]> wrote:

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.

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
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top