【问题标题】:Popen Executes At the Wrong TimePopen 在错误的时间执行
【发布时间】:2019-11-23 18:00:35
【问题描述】:

我正在编写一个 python 脚本,当我运行代码时,我运行子进程之后的所有代码都会在子进程本身之前执行。因此,例如这里程序将在子进程之前运行 print("blahblafkenrferkfnrnkr")。

如何在subprocess.Popen("timeout 60 python2 script.py", shell=True, executable="/bin/bash") 执行(for 60 seconds as I am trying to do) 之后使程序运行print("blahblafkenrferkfnrnkr")

print("blahblh")

subprocess.Popen("timeout 60 python2 script.py", shell=True, executable="/bin/bash")

print("blahblafkenrferkfnrnkr")

【问题讨论】:

    标签: python python-3.x subprocess popen


    【解决方案1】:

    Popen 启动一个在后台执行的进程,您无需等待它完成。要么:

    1. 使用等待您的Popen 的高级包装器之一(例如,默认为subprocess.run,或subprocess.check* 系列之一)
    2. 存储生成的流程对象并在其上调用waitcommunicate 也可以,但由于您不发送/捕获输入/输出,因此没有必要)

    #2 的示例只是:

    print("blahblh")
    
    proc = subprocess.Popen("timeout 60 python2 script.py", shell=True, executable="/bin/bash")
    proc.wait()
    
    print("blahblafkenrferkfnrnkr")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多