【问题标题】:Python subprocess call is not running in orderPython子进程调用未按顺序运行
【发布时间】:2022-08-19 00:29:37
【问题描述】:

我正在尝试编写一个调用 bash 脚本的 python 文件。我有以下代码

Print (\"hello before subprocess call\")
subprocess.call([\'/builds/adummyscript.sh\'], shell=True)
Print (\"hello after subprocess call\")

虚拟脚本只是一个带有以下代码的 bash 脚本

echo \"hello from the inside of the subprocess call\"

我的代码的输出是

hello from the inside of the subprocess call
hello before subprocess call
hello after subprocess call

我的问题是为什么子进程在打印语句之前运行,我该如何解决这个问题?

    标签: python python-3.x bash subprocess


    【解决方案1】:

    该程序在您的 print 语句之后运行,python 只是在调用子进程之前不会刷新其标准输出缓冲区。调用脚本时使用flush=True 刷新python 标准输出或使用无缓冲的python。

    print("hello before subprocess call",flush=True)
    

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多