如何监控子流程对象?
How to monitor the subprocess object?
我需要使用在 bazel 上运行的 mediapipe 编译器转换大量文件。有数百个文件,所以这个过程必须自动化。正常执行的命令是这样的:
GLOG_logtostderr=1 bazel-bin/mediapipe/examples/desktop/multi_hand_tracking/multi_hand_tracking_cpu --input_video_path=/home/tony/Videos/HandWashDataset/Step_1/HandWash_001_A_01_G01.mp4
其中 GLOG_logtostderr 是附加到 bazel 程序以输出日志(结果)的记录器。我在最后使用了 redirect (2>a.txt) 来将结果写入 txt 文件。
我通过使用子进程模块编写 Python 脚本使程序正常运行。
import glob
import os
import time
import subprocess
files = glob.glob("/home/tony/Videos/HandWashDataset/Step_1/*.mp4")
os.chdir("/home/tony/mediapipe")
output = ""
for i in files:
print("process file {}".format(i))
output = (i[:len(i)-4]) + ".txt"
inputf = "--input_video_path=" + i
outputf = "2>" + output
f = open("blah.txt", "w")
sp = subprocess.call(["GLOG_logtostderr=1 bazel-bin/mediapipe/examples/desktop/multi_hand_tracking/multi_hand_tracking_cpu " + inputf], shell=True, stderr=f)
time.sleep(180)
f.close()
print("process finished")
我遇到的问题是目前似乎无法控制每次迭代的过程。因为它正在调用脚本中的另一个程序。对 sp 的调用似乎几乎是即时的,但实际转换实际上需要几分钟。如果没有 time.sleep,bazel 的所有实例都会立即启动并杀死我的计算机。有没有一种方法可以监控进程以便一次转换一个文件?
你应该使用 subprocess.run
:
运行args描述的命令,
等待命令完成,
然后 returns 一个 CompletedProcess 实例。
我需要使用在 bazel 上运行的 mediapipe 编译器转换大量文件。有数百个文件,所以这个过程必须自动化。正常执行的命令是这样的:
GLOG_logtostderr=1 bazel-bin/mediapipe/examples/desktop/multi_hand_tracking/multi_hand_tracking_cpu --input_video_path=/home/tony/Videos/HandWashDataset/Step_1/HandWash_001_A_01_G01.mp4
其中 GLOG_logtostderr 是附加到 bazel 程序以输出日志(结果)的记录器。我在最后使用了 redirect (2>a.txt) 来将结果写入 txt 文件。
我通过使用子进程模块编写 Python 脚本使程序正常运行。
import glob
import os
import time
import subprocess
files = glob.glob("/home/tony/Videos/HandWashDataset/Step_1/*.mp4")
os.chdir("/home/tony/mediapipe")
output = ""
for i in files:
print("process file {}".format(i))
output = (i[:len(i)-4]) + ".txt"
inputf = "--input_video_path=" + i
outputf = "2>" + output
f = open("blah.txt", "w")
sp = subprocess.call(["GLOG_logtostderr=1 bazel-bin/mediapipe/examples/desktop/multi_hand_tracking/multi_hand_tracking_cpu " + inputf], shell=True, stderr=f)
time.sleep(180)
f.close()
print("process finished")
我遇到的问题是目前似乎无法控制每次迭代的过程。因为它正在调用脚本中的另一个程序。对 sp 的调用似乎几乎是即时的,但实际转换实际上需要几分钟。如果没有 time.sleep,bazel 的所有实例都会立即启动并杀死我的计算机。有没有一种方法可以监控进程以便一次转换一个文件?
你应该使用 subprocess.run
:
运行args描述的命令, 等待命令完成, 然后 returns 一个 CompletedProcess 实例。