Controling the execution of external programs on Win2K

Ben Hutchings ben.hutchings at roundpoint.com
Wed Mar 28 17:46:33 EST 2001


"Ben" <bencr at bigpond.com> writes:

> Hi,
> 
> I placed a post last night looking for a way to run exe's from a Python
> programme, where the Python programme waits for each instance of the exe to
> finish/return and exit code before proceding with it's next command.
> 
> As suggested I have looked at os.system( ) but on Win2K it seems every
> external program I execute spawns an external command shell and there
> doesn't seem a way to prevent this behaviour ... which is not good (atleast
> from what I am trying to do) ...  after a bit more investigation I believe
> it is possible to achieve this using the win32process module. I am just
> unsure of what args to pass to make it behave in this way.
<snip>

You can't do that directly with the CreateProcess function.  If you
were using the Win32 API directly you would call WaitForSingleObject
or WaitForMultipleObjects, passing the process handle it returns.
In Python you could use something like this:

    def CreateProcess(commandline, wait):
        import win32event, win32process
        process, thread, pid, tid = win32process.CreateProcess(
            None,
            commandline,
            None,
            None,
            0,
            win32process.NORMAL_PRIORITY_CLASS,
            None,
            None,
            win32process.STARTUPINFO())
        if wait:
            win32event.WaitForSingleObject(process, win32event.INFINITE)

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list