[英]Python subprocess update child-process
Is it possible to update portions of a subprocess spawned via Popen([cmd], stdin=PIPE)
? 是否可以更新通过Popen([cmd], stdin=PIPE)
生成的子流程的某些部分?
I've been trying to build a script that interacts with a GUI for updating files. 我一直在尝试构建与GUI交互以更新文件的脚本。 Here is what I've been toying with: 这是我一直在玩的东西:
def __init__(self):
#... <SNIPPED>
process = self.outer._display(self.custom['dialog_name'], _passed, return_process=True)
# Returns executed list, example -> ['/bin/foo', '--bar', '0', '--lyn', 'twenty']
self.pipe = subprocess.Popen(process, stdin=subprocess.PIPE)
The pipe is built in the __init__
of my class. 管道是在我的类的__init__
中构建的。 I want to be able to run the following function to update the process. 我希望能够运行以下功能来更新过程。
def update(self, bar, lyn):
self.pipe.communicate('--bar {} --lyn {}\n'.format(bin, lyn))
After running the update function after initializing the class containing the pipe , the subprocess is not updated. 在初始化包含管道的类之后运行更新功能后,不会更新子流程。 I'm pretty new to Python, so I wouldn't be surprised if I was approaching this the wrong way. 我对Python还是很陌生,所以如果我以错误的方式使用它,我也不会感到惊讶。
If you can either help explain what I'm doing wrong or a better solution to this issue, please let me know. 如果您可以帮助解释我在做什么错,或者可以更好地解决此问题,请告诉我。
I've been able to achieve what I want via os.popen, but I rather use the preferred (subprocess) 我已经能够通过os.popen实现我想要的东西,但是我宁愿使用首选的(子过程)
Each os.popen()
starts a new shell process. 每个os.popen()
启动一个新的Shell进程。 self.pipe.communicate()
doesn't start a new process: it writes all input, reads all output, and waits for the process to finish. self.pipe.communicate()
不会启动新进程:它将写入所有输入,读取所有输出,并等待该进程完成。
To start a command with a new set of parameters: 要使用一组新的参数启动命令:
def update(self, bar, lyn):
cmd = ['/bin/foo', '--bar', bar, '--lyn', lyn]
self.processes.append(subprocess.Popen(cmd))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.