使用python以“终端模式”启动程序并添加命令

问题描述 投票:1回答:1

我使用子进程模块以“终端模式”启动程序,方法是在可执行文件之后添加一个标志,如下所示:

subprocess.call(nuke + " -t ")

这导致终端模式,因此所有后续命令都在程序的上下文中(我的猜测是它是programms python解释器)。

Nuke 11.1v6, 64 bit, built Sep  8 2018. Copyright (c) 2018 The Foundry Visionmongers Ltd.  All Rights Reserved. Licence expires on: 2020/3/15
>>>

如何从启动终端模式的python脚本继续向解释器推送命令?你会如何从脚本中退出这个程序解释器?

编辑:

nuketerminal = subprocess.Popen(nuke + " -t " + createScript)
nuketerminal.kill()

在加载python解释器之前终止进程并且脚本执行任何关于如何优雅地解决这个问题的想法没有延迟?

python subprocess
1个回答
1
投票
from subprocess import Popen, PIPE

p = subprocess.Popen([nuke, "-t"], stdin=PIPE, stdout=PIPE) # opens a subprocess
p.stdin.write('a line\n') # writes something to stdin
line = p.stdout.readline() # reads something from the subprocess stdout

不同步读取和写入可能会导致死锁,即。当主进程和子进程都等待输入时。

您可以等待子进程结束:

return_code = p.wait() # waits for the process to end and returns the return code
# or
stdoutdata, stderrdata = p.communicate("input") # sends input and waits for the subprocess to end, returning a tuple (stdoutdata, stderrdata).

或者您可以使用以下命令结束子流程:

p.kill()
© www.soinside.com 2019 - 2024. All rights reserved.