2

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?

anta40
  • 6,511
  • 7
  • 46
  • 73
  • your question is too long. make your question short and sharp. – LovaBill Sep 04 '15 at 12:19
  • Hi William, I just edited my question. – anta40 Sep 14 '15 at 08:14
  • Hi! Since you play the video, the problem is killing it. Have you tried to kill your video with hardcoded timestamps? Have you tried to kill the video at all? It might be the `os.kill` or your time handling that fails to work properly. You must narrow further down your problem by yourself and then if something is really wrong, try asking again. – LovaBill Sep 16 '15 at 07:28

0 Answers0