简体   繁体   English

Python子流程更新子流程

[英]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.

相关问题 在Visual Studio Code中运行Node.js,将python脚本作为子进程调用并对其进行调试 - In Visual Studio Code run Node.js, call python script as child-process and debug it 从带有子进程的nodejs app调用时,python无法正常工作 - python not working correctly when called from nodejs app with child-process Python子进程被子进程卡住 - Python subprocess gets stuck by a child process Python子进程有效地检查子进程是否终止 - Python Subprocess efficiently check if child process terminated Python子进程,给子进程一个输入 - Python subprocess, give an input to a child process 使用“子进程”从 python 脚本中检索数据适用于普通 node.js 脚本,但不适用于必需的脚本 - Retrieving data from python script using 'child-process' works in normal node.js script, but not in a required script Python子进程模块:从父进程中的子进程捕获异常 - Python subprocess module: Catch exception from a child process in parent process 如何使用 Python 子进程捕获子进程的错误? - How to catch the errors of a child process using Python subprocess? Python. 连续 io eschange with subprocess.Popen() child process - Python. Continuous io eschange with subprocess.Popen() child process 防止在Python中使用子流程创建新的子流程 - Prevent creating new child process using subprocess in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM