【问题标题】:Output from two processes on shell (Python,Linux)shell 上两个进程的输出(Python、Linux)
【发布时间】:2015-07-22 13:56:15
【问题描述】:

我的 python 脚本执行在 shell 上发送其输出的程序。 但是,脚本应该同时执行它自己的命令,这些命令在 shell 上也有输出。 有可能这样做吗?我会看到脚本和进程的输出吗? 这是在 shell 上有输出的进程:

s.exe_cmd('./upgrade')

那么,我能写吗

print "my output..."  

并在 shell 上看到它?

【问题讨论】:

  • 为什么不试试看呢?
  • “将其输出发送到 shell”绝对没有意义。 shell只是一个进程。您的交互式 shell 的标准输出与 tty 相关联,并且它的所有子级都继承该文件描述符,除非重定向。如果两个进程都向 tty 写入数据,则 tty 将按照接收到的顺序显示数据。

标签: python linux shell


【解决方案1】:

假设您使用的是subprocess - 它会在控制台上同时打印。

import subprocess

p = subprocess.Popen("sleep 1 && echo from inside subprocess",
                     shell=True)
print("will print asynchronously")
p.wait()

这是一个您可以试用的示例程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多