Wait until process completes using Python WMI
<p>My code launches process on remote host that at the end creates txt file. I want to copy this txt file so I need to wait until the process finish. How can I do that?</p>

<pre><code>import wmi
SW_SHOWNORMAL = 1
con = wmi.WMI(ip, user=username, password=password)
process_startup = con.Win32_ProcessStartup.new()
process_startup.ShowWindow = SW_SHOWNORMAL
process_id, result = con.Win32_Process.Create(CommandLine=command_line, ProcessStartupInformation=process_startup)
</code></pre>

<p>I'm actually working on a similar thing at the moment. If you haven't found your answer, check this link - <a href="http://timgolden.me.uk/python/wmi/cookbook.html#run-notepad-wait-until-it-s-closed-and-then-show-its-text" rel="nofollow">http://timgolden.me.uk/python/wmi/cookbook.html#run-notepad-wait-until-it-s-closed-and-then-show-its-text</a></p>

<p>My current Python solution is as follows:</p>

<pre><code>process_id, result = client.Win32_Process.Create(CommandLine=command, CurrentDirectory=directory)
watcher = client.watch_for(
    notification_type="Deletion",
    wmi_class="Win32_Process",
    delay_secs=1,
    ProcessId=process_id
)

watcher()
</code></pre>

<p>This seems to work for me.</p>

python process wmi