pywinauto possible to keep application alive if I exit python script?

Viewed 259
app = Application(backend="uia").start("program.exe")

I am using pywinauto to do some tasks indefinitely. However, occasionally, I need to restart the script for some external reasons. When this happens, I would like to keep the created applications open. How can I do this? I noticed if the python script errors out, the applications will stay open. But if I exit the script manually, the windows will close. So there must be some way to accomplish this.

3 Answers

I think pywinauto's Application.start cannot do the work. You can try:

pid = os.spawnl(os.P_NOWAIT, "program.exe")
app = Application().connect(process=pid)

os.spawnl is considered deprecated. Use subprocess module.

Combining the answer in Run a program from python, and have it continue to run after the script is killed and pywinauto official doc, you can do this:

subprocess.Popen(
    ['your_program', 'with args'],
    # These will make sure the desktop program will alive even when shell session terminates.
    creationflags=subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP, shell=True
)
desktop = Desktop(backend="uia")
main_win = desktop.window(title="program's window title", control_type="Window")

Why not use system command:

#Use multiple thread to avoid block of system function 
import _thread as qd 
import os
qd.start_new_thread(os.system,('notepad',))

from pywinauto import Application
#connect pywinauto with application via title regular expression
win=Application(backend='uia').connect(title_re='.*Notepad.*')

Then you use pywinauto to connect application via title?

Related