简体   繁体   English

如何使用Python Popen()与同一进程持续通信

[英]How to continually communicate with same process with Python Popen()

I am new at this so hope you understand... 我是新来的,希望您能理解...

Now I am using python subprocess module for sending the specific command. 现在,我正在使用python子进程模块发送特定命令。

After I send the command1 with Popen function, I would like to send a command2 to same process one more. 使用Popen函数发送command1之后,我想再次将command2发送到同一进程。

Is it possible...? 可能吗...?

// Example
// command1 -> Opening command for text program.
// command2 -> Executing the write and save command.(on above text program) 

popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
// next....?

please let me know If I understand it incorrectly. 如果我理解不正确,请告诉我。

Check this out. 看一下这个。 In unix, we can execute multiple commands in one go like 在Unix中,我们可以一次性执行多个命令,例如

cmd_1;cmd_2

We will use same thing with sub-process. 我们将在子流程中使用相同的东西。

Make sure to use shell=True . 确保使用shell=True

import subprocess
all_cmd = "echo a;echo b;echo c"

p = subprocess.Popen(all_cmd,stdout=subprocess.PIPE, shell=True)
p_stdout,p_err = p.communicate()
print p_stdout.strip()

It is perfectly doable but you might want to look into something like pexpect which offers more control over the dialog. 这是完全可行的,但您可能希望研究像pexpect这样的东西,它可以提供对对话框的更多控制。

cmd = subprocess.Popen(['command1'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  # no shell=True here!
stdout, stderr = cmd.communicate('command2')

This presumes that command1 will keep running and reading additional commands on its standard input. 这假定command1将继续运行并在其标准输入上读取其他命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 3 Popen(半)交互式来回通信与进程Popen.communicate()vs Popen.stdin / Popen.stdout - python 3 Popen (semi-)interactive back-and-forth communication with a process, Popen.communicate() vs Popen.stdin/Popen.stdout 如何杀死由 popen 创建并使用通信()的进程? - How to kill a process which is create by popen and uses communicate()? Python子进程交互,为什么我的进程与Popen.communicate一起工作,而不是Popen.stdout.read()? - Python subprocess interaction, why does my process work with Popen.communicate, but not Popen.stdout.read()? 如何使用popen创建管道并在Python中通过管道进行通信? - how do I create a pipe using popen and communicate through it in Python? 如何使用python popen打印并存储通讯和返回代码? - how to use python popen to print and also store communicate and return code? 如何让二级进程通过python中的ProcessPoolExecutor不断更新变量 - How to make a secondary process continually update a variable through ProcessPoolExecutor in python 弹出失败后使用process.communicate - Using process.communicate after popen fails 如何终止由 Python Popen 创建的进程 - How to terminate process created by Python Popen 有没有一种方法可以使用python子进程模块的Popen.communicate()而无需等待进程退出? - Is there a way to use the python subprocess module's Popen.communicate() without waiting for the process to exit? Python2:使用Popen.communicate()编写交互式过程的stdin,而无需尾随换行符 - Python2: Writing to stdin of interactive process, using Popen.communicate(), without trailing newline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM