A friend of mine asked me to wrote a simple code to play a video each time a motion is detected. The hardwares used are a Raspberry Pi B+ and a Logitech C170 webcam.
This is full code: http://pastebin.com/Z6nS9MXf. Overall, it works almost as expected.
So the issue is the timeout_command part:
def timeout_command(command, timeout):
cmd = command.split(" ")
start = datetime.datetime.now()
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while process.poll() is None:
time.sleep(0.1)
now = datetime.datetime.now()
if (now - start).seconds > timeout:
os.kill(process.pid, signal.SIGKILL)
os.waitpid(-1, os.WNOHANG)
return None
return process.stdout.read()
It's supossed to do something within a timeout period, and after the period is reached, then the process is stopped.
I wrote this in the code:
timeout_command("omxplayer /home/pi/video/trololo.mp4", 10)
Which is supposed to make omxplayer play the video for only 10 seconds, then close after that. Yet omxplayer plays the video till the end.
How to fix this?