waiting for a program run by popen3() to end

Discussion in 'Python' started by Rajarshi Guha, Oct 14, 2003.

  1. Hi,
    I have some code that runs an interactive program via popen3(). The program generates
    output which is then used later on. My script looks like this:



    o,i,e = os.popen3('/usr/local/adapt/bin/descmng -g')
    o.write('\n%s\n\n' % (redpick))
    o.close()
    time.sleep(1)
    os.system('mv set5 gendes.in && rm set? outputdesc.txt')

    The program (descmng) produces the files set?, outputdesc.txt

    However on some systems, the program takes more than 1 sec to complete
    and as a result the os.system() command fails.

    Increasing the sleep time to 5 sec solves the problem but it does'nt seem to
    be reliable.

    I have considered using fork() and wait() but that does'nt let me run the
    program interactively.

    Is there any other way out (apart from dumping commands to a file and
    piping them to my program in a fork())?

    Thanks,
     
    Rajarshi Guha, Oct 14, 2003
    #1
    1. Advertising

  2. Rajarshi Guha

    Donn Cave Guest

    Quoth Rajarshi Guha <>:

    | I have some code that runs an interactive program via popen3(). The program generates
    | output which is then used later on. My script looks like this:
    |
    |
    |
    | o,i,e = os.popen3('/usr/local/adapt/bin/descmng -g')
    | o.write('\n%s\n\n' % (redpick))
    | o.close()
    | time.sleep(1)
    | os.system('mv set5 gendes.in && rm set? outputdesc.txt')
    |
    | The program (descmng) produces the files set?, outputdesc.txt
    |
    | However on some systems, the program takes more than 1 sec to complete
    | and as a result the os.system() command fails.
    |
    | Increasing the sleep time to 5 sec solves the problem but it does'nt seem to
    | be reliable.
    |
    | I have considered using fork() and wait() but that does'nt let me run the
    | program interactively.
    |
    | Is there any other way out (apart from dumping commands to a file and
    | piping them to my program in a fork())?

    You should look at the popen2 module in the library. You're using
    it already, via os.popen3, but the classes in popen2.py provide more
    features. However, it seems to me that you should be able to synch
    with the child process by simply reading from its output or error
    files. Whether it actually outputs any data or not, when the process
    exits, those pipes should close and the read will finish. If you're
    not interested in the data, though, I would not redirect error output
    and throw it away as you're doing here, and if you redirect only the
    regular output, that will greatly reduce odds of a deadlock.

    Or even simpler, you can add the other statements to the command
    to be run by os.popen3.

    Donn Cave,
     
    Donn Cave, Oct 15, 2003
    #2
    1. Advertising

  3. Rajarshi Guha

    Miki Tebeka Guest

    Hello Rajarshi,

    > o,i,e = os.popen3('/usr/local/adapt/bin/descmng -g')
    > o.write('\n%s\n\n' % (redpick))
    > o.close()
    > time.sleep(1)
    > os.system('mv set5 gendes.in && rm set? outputdesc.txt')

    If you're not reading the program output you can use os.system which
    will wait for it to end.

    > Is there any other way out (apart from dumping commands to a file and
    > piping them to my program in a fork())?

    On Unix like systems there is popen2.Popen3 (or Popen4)
    ---
    from popen2 import Popen3

    print "Running child"
    p = Popen4("my_process arg1 arg2")
    print "Waiting"
    p.wait() # Will wait for process to end
    print "And we got:"
    print p.fromchild.read()
    ---

    HTH.
    Miki
     
    Miki Tebeka, Oct 15, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrea B.
    Replies:
    4
    Views:
    918
    Olivier Merigon
    Aug 31, 2004
  2. Replies:
    2
    Views:
    364
  3. Luigi
    Replies:
    5
    Views:
    416
    Donn Cave
    Apr 3, 2006
  4. Alessandro
    Replies:
    6
    Views:
    2,292
    Arne Vajhøj
    Mar 4, 2009
  5. Alex Young

    Pid of the other end of a popen3?

    Alex Young, Mar 25, 2011, in forum: Ruby
    Replies:
    3
    Views:
    209
    Roger Pack
    Mar 28, 2011
Loading...

Share This Page