tonywang
tonywang

Reputation: 191

How to monitor the subprocess object?

I need to convert a lot of files using mediapipe compiler which runs on bazel. There are hundreds of files so this process has to be automated. The command to be executed normally would be something like:

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

Where GLOG_logtostderr is the logger attached to the bazel program to output the log(result). I used redirect (2>a.txt) at the end to get the results to be written as txt file.

I have got the program working by writing a Python script using subprocess module.

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")

The problem I am having is currently it seems to have no control on the process in each iteration. Since it is invoking another program in the script. The call to sp seems to be nearly instant but the actual conversion actually takes a few minutes. Without time.sleep, all instances of the bazel are launched at once and killed my computer. Is there a way to monitor process so I can convert one file at a time?

Upvotes: 0

Views: 250

Answers (1)

bitranox
bitranox

Reputation: 1884

You should use subprocess.run :

That runs the command described by args, waits for the command to complete, then returns a CompletedProcess instance.

see also : https://docs.python.org/3/library/subprocess.html

Upvotes: 2

Related Questions