subprocess confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Arnold

    #1

    subprocess confusion

    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.gethostn ame():
    shname = None
    else:
    shname = lambda :'/bin/remsh %s ' % (machine)
    p = subprocess.Pope n(preexec_fn = shname,
    shell = True,
    args = command,
    stderr = subprocess.STDO UT,
    stdout = log,
    env = env,
    )
    try:
    p.wait()
    if log:
    log.close()
    except:
    pass

    -------------------------------


  • Michael Hoffman

    #2
    Re: subprocess confusion

    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.gethostn ame():
    command = "/bin/remsh %s %s" % (machine, command)

    subprocess.chec k_call(command, shell=True, env=env,
    stderr=subproce ss.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

    Comment

    • Nick Craig-Wood

      #3
      Re: subprocess confusion

      Tim Arnold <tiarno@sas.com wrote:
      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.s etsid

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

      --
      Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

      Comment

      • Tim Arnold

        #4
        Re: subprocess confusion

        "Nick Craig-Wood" <nick@craig-wood.comwrote in message
        news:slrnf29k6m .c97.nick@irish sea.home.craig-wood.com...
        Tim Arnold <tiarno@sas.com wrote:
        <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.s etsid
        >
        You seem to be thinking it is pre-pending something to your command
        line which isn't how it works.
        >
        --
        Nick Craig-Wood <nick@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


        Comment

        Working...