我正在调用子进程(希望脚本运行一些命令)并且需要等到它完成才能启动另一个函数,但是我认为它只等到shell命令结束,而不是期望脚本。 是否有可能等待整个过程完成?
p111 = subprocess.Popen('gnome-terminal -e "perl /tmp/expect',shell=True)
os.waitpid(p111.pid,0)
smo_final()
答案 0 :(得分:1)
使用
https://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait
p11 = subprocess.Popen('gnome-terminal -e "perl /tmp/expect',shell=False)
p11.wait()
答案 1 :(得分:0)
使用pexpect,python expect implementation
import pexpect
...
child = pexpect.spawn(cmd) # the command you want to run
child.expect(str_expect) # the string you expect
child.sendline(str_response) # the string with which you'd like to respond
child.wait() # wait for the child to exit
...
# the rest of your code here