If you are not opposed to installing Python Win32 extensions , you can use the Windows API to wait for the process to complete:
from win32con import SYNCHRONIZE from win32event import WaitForSingleObject, INFINITE from win32api import OpenProcess, CloseHandle process_handle = OpenProcess(SYNCHRONIZE, False, pid) try: WaitForSingleObject(process_handle, INFINITE) finally: CloseHandle(process_handle)
This will block the code until the process ends (the INFINITE value in the WaitForSingleObject() value can be changed if desired, see the documentation ).
Alternatively, without installing the extension, you can do this with ctypes , especially easily, since all the necessary methods are located in kernel32.dll :
from ctypes import windll SYNCHRONIZE = 0x100000L INFINITE = 0xFFFFFFFFL process_handle = windll.kernel32.OpenProcess(SYNCHRONIZE, 1, pid) windll.kernel32.WaitForSingleObject(process_handle, INFINITE) windll.kernel32.CloseHandle(process_handle)
Of course, in this case the error handling code (omitted) is a bit more complicated and much smaller than Pythonic.
source share