python-3.x - Python3 从子进程读取标准输出
问题描述
我想将一个进程作为子进程启动。python 脚本仍应从外部获取控制台输入以控制子进程发生的情况。为此,我想定期将子进程的输出打印到控制台,然后再读取控制台输入。这是我目前的代码:
def start():
child_proccess = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
def check():
print("some tutorial string")
lines = []
for line in io.TextIOWrapper(child_proccess.stdout, encoding="utf-8"):
lines.append(line)
for line in lines[-10:]:
print(line)
start()
while True:
check()
time.sleep(1)
这里的问题是,通过 stdout 的迭代会以某种方式阻塞,这意味着第二个 for 循环永远不会执行并且控制台保持空白。
解决方案
推荐阅读
- cryptography - 如何获取 OID 值的 OID 描述?
- google-bigquery - 从 Julian 转换为 gregorian Bigquery
- c# - 如何在 C# mvc 中将 3 个参数路由到控制器中?
- android - 在图标旁边设置 BottomNavigationView 标题
- jenkins-pipeline - 如何在多个平台上并行运行单个阶段列表
- javascript - 使用 EWS 请求获取带有内容的附件内容(文件)
- java - 构建一个 jar 以包含未获得执行权限的 shell 脚本文件
- boot - 启动 Google Coral,未知块(179,3)错误
- node.js - 'multer' 在示例代码中有效,但在我的项目中无效
- c++ - C++ 应用程序访问冲突 - 使用 CoInitializeEx 初始化 COM