#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0

    Question is there way to stop execution of pythonscript until it finish execution of .exe?


    Hello everybody,

    I am trying to run .exe file from python script and i want to stop further execution of python script untill .exe file is closed.

    is there any command or way to do this? I tried to use system command. but after it opens .exe file, it goes to next statement for further execution. it want stop until .exe file is closed.

    Everything is welcome.

    Thanks in Advance
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You're best bet would be to use os.popen(), which i believe will start and stay with the current process untill finished. Though im pretty sure os.system() will do this - can't say i've really tried to start an exe and wait for processes before sorry.

    Now Pythons homepage is back up heres the link to the OS module:

    http://www.python.org/doc/2.3.4/lib/module-os.html

    Hope this helps,

    Mark.
    Last edited by netytan; July 12th, 2004 at 11:29 PM. Reason: Adding link to 'os' as promised
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0

    Question


    Hii Mark,

    I tried using os.popen(). It is just like os.system(). It want stop to wait for .exe program to stop or closed.

    Please any other way. is there any other command to wait for .exe program to finish in accordance with os.popen or os.system? Like wait()...

    Thanks. Waitng for reply...

    Devhims
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There is time.sleep() but unless you know how long the exe is going to run for it probably wont do you any good. Of course, if you new the program was going to run for at the most 30 seconds you could make Python sleep for that long.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    How about something like this:
    Code:
    import popen2
    buff = 1024 #A nice big number to take any possible output
                     #in between reads
    proc = popen2.Popen4("MyProg.exe",buff)
    
    while proc.poll() == -1:
        print "My prog is still running"
        And do something useful .....
    
    print "The program finished with exit code", proc.poll()
    It works very well under Linux and Windows and you have the advantage of being able to read it's output with the fromchild method.

    grim
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    Belgium
    Posts
    3
    Rep Power
    0

    Re: execution stop until execution finished


    You could use os.fork() to create a child process. The child would then os.popen() or whatever to execute your external app and the parent would waitpid() (after doing some optional work if it needs to), waiting for the child to os.exit() . I don't know if it works on windows, though.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    os.system() should always wait for the program to finish - in fact it has to on most systems, since it returns the exit code. Are you sure it is continuing? Can you show us your code? What OS are you running on?

    Dave - The Developers' Coach
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    27
    Rep Power
    0

    Question


    Thanks to everyone for their reply. Now it is working . I used
    os.spawnl(mode,...) command. with mode = os.P_WAIT.

    But sometime it hangs computer and you have to close pyton program manually. As soon as you closed it, (.exe)program will open on which python was spawn. I do not why it does that. May be due to lack of memory.

    Most of the time it works fine.

    Still i will try with os.fork() and let you guys know.

    One more by any chance anybody know how to minimize opened .exe file or run it in background.? So, end-use want know from where it is running.

    DevHims

IMN logo majestic logo threadwatch logo seochat tools logo