-1

I've been using Python to run a video processing program on a large collection of .mp4 files. The video processing program (which I did not write and can't alter) does not exit once it reaches the final frame of the video, so using os.system(cmd) in a loop going through all the .mp4 files didn't work for me unless I sat there killing the processing program after each video ended.

I tried to solve this using a subprocess that got terminated after the video ended (a predetermined amount of time):

for file in os.listdir(myPath):
    if file.endswith(".mp4"):
        vidfile = os.path.join(myPath, file)
        command = "./Tracking " + vidfile
        p = subprocess.Popen(command, shell=True)
        sleep(840)
        p.terminate()

However, the Tracking program still doesn't exit, so I end up with tons of videos open at the same time. I can only get rid of them by force quitting each separate frame or by using kill -9 id for the id of that particular instance of the program. I've read that using shell=True isn't recommended, but I'm not sure if that would cause this behavior.

How can I kill the Tracking program after a certain amount of time? I'm extremely new to Python and am not sure how to do this. I was considering doing something like os.system("kill -9 id") after the sleep(), but I don't know how to get the id of the program either.

anodi
  • 1
  • 2
  • Use .kill() and see if you have better luck. Also, what system are you running on? – Games Brainiac Oct 16 '14 at 00:00
  • You can kill a program by name, you don't need the pid – Padraic Cunningham Oct 16 '14 at 00:05
  • `shell=True` _could_ cause this behavior, depending on your platform. When you use it, you don't have a handle to the actual process, you have a handle to the shell. But really, why do you _want_ to use `shell=True` here? Why not just do it the right way, which is just as easy as doing it the wrong way, and see what happens? – abarnert Oct 16 '14 at 00:12
  • Meanwhile, does `Tracking` output anything to stdout or stderr that you can use, or is timing really the only possibility (short of watching file handles or something)? – abarnert Oct 16 '14 at 00:13

1 Answers1

2

Drop shell=True, use p.kill() to kill the process:

import subprocess
from time import time as timer, sleep

p = subprocess.Popen(["./Tracking", vidfile])
deadline = timer() + 840
while timer() < deadline:
    if p.poll() is not None: # already finished
        break
    sleep(1)
else: # timeout
    try:
        p.kill()
    except EnvironmentError:
        pass # ignore errors
    p.wait()

If it doesn't help then try to create a new process group and kill it instead. See How to terminate a python subprocess launched with shell=True.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • When I do this, I get an error for the `subprocess.Popen`: `File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory` – anodi Oct 16 '14 at 03:16