July 12th, 2004, 10:47 AM
is there way to stop execution of pythonscript until it finish execution of .exe?
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
July 12th, 2004, 11:41 AM
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:
Hope this helps,
Last edited by netytan; July 13th, 2004 at 12:29 AM.
Reason: Adding link to 'os' as promised
July 12th, 2004, 01:08 PM
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...
July 12th, 2004, 01:13 PM
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.
July 12th, 2004, 02:44 PM
How about something like this:
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.
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()
July 12th, 2004, 04:05 PM
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.
July 12th, 2004, 04:54 PM
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
July 12th, 2004, 06:35 PM
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.