Python等待子进程在终止之前完成

时间:2018-05-03 14:50:27

标签: python subprocess wait pyautogui

我正在开发一个python脚本,它使用子进程模块打开一个程序,然后使用pyautogui模块向程序发送键盘快捷键。但是,当程序打开并向其发送CTRL + O快捷方式时,程序将打开一个文件,然后对该文件进行一些处理。

有没有办法,使用python中的子进程模块等待程序完成处理(但仍在运行),然后在最终终止程序/进程之前输入CTRL + S快捷方式(使用pyautogui)?

以下是我使用的代码示例:

from pyautogui import press, typewrite, hotkey, moveTo
from subprocess import Popen, PIPE, STDOUT
import time
import os   

def saveNClose(program, out_filename):
    # open the save file dialog
    hotkey('ctrl', 's')

    #wait for save file dialog to appear 
    time.sleep(0.5)

    # enter the file path of the file to save
    typewrite(out_filename, interval=0.01)

    # save to file
    press('enter')

    #close kurzweil
    program.terminate()

def openProgram(in_fname, out_fname, wait_time):

    #open kurzweil
    program = Popen([r"C:\Program Files (x86)\Kurzweil Educational 
    Systems\Kurzweil 3000\Kurzweil 3000.exe"], stdout=PIPE, stdin=PIPE, 
    stderr=STDOUT, shell=False)

    #wait for login screen to appear
    time.sleep(2)

    #login with creditentials Here

    press('enter') #login

    #wait for kuzweil to load
    time.sleep(8)

    #open the open file dialog
    hotkey('ctrl', 'o')

    #wait for open file dialog to appear
    time.sleep(1)

    #enter the file path of the file to open
    typewrite(in_fname, interval=0.01)

    #open the file
    press('enter')

    #wait for the conversion options to appear
    time.sleep(0.5)

    # confirm conversion options
    press('enter')

    # Program Does Processing Here

    #**************************************************
    # Need help Here to wait until processing is done.
    #**************************************************

    #Then input CTRL + S and Terminate.
    saveNClose(program, out_fname)

0 个答案:

没有答案