繁体   English   中英

python 向正在运行的进程发送参数

[英]python sending argument to a running process

I have a flutter project called zed , my goal is to monitor the output of flutter run , as long as pressing r , the output will increase.

在此处输入图像描述

为了自动实现这个工作流程,我的实现是

import subprocess

bash_commands = f'''
cd ../zed
flutter run  --device-id web-server --web-hostname 192.168.191.6 --web-port 8352
'''

process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=False)
output, err= process.communicate(bash_commands.encode('utf-8'))
print(output, err)
output, _ = process.communicate('r'.encode('utf-8'))
print(output)

它没有像我预期的那样工作,屏幕上没有打印任何内容。

使用 process.stdin.write() 而不是 process.communicate()

process.stdin.write(bash_commands)
process.stdin.flush()

但你为什么问

Popen.communicate(input=None, timeout=None)与进程交互:向标准输入发送数据。 从 stdout 和 stderr 读取数据,直到到达文件结尾

在 pipe 关闭之前, communicate(...)不会返回,这通常发生在子进程关闭时。 非常适合ls -l对于长时间运行的子进程不太好。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM