python - Popen.kill() failing -
update 2: piped output of stderr , looks when include shell=true, file omx player (it lists command line switches , such). possible shell=true might not play nicely omxplayer?
update: came across link before failed on me moved on without digging deeper. after tshepang suggested again looked further. have 2 problems, , i'm hoping first caused second. first problem when include shell=true arg, video never plays. if don't include it, video plays, not ever killed. updated code below.
so trying write python app raspberry pi plays video on loop (i came across popen way accomplish using omxplayer) , on keyboard interrupt, kills process , opens process (playing different video). eventual goal able use vid1 sort of "screensaver" , have vid2 play when user interacts system, im trying kill vid1 on keyboard input , running quite hard time doing it. i'm hoping can tell me code falling down.
forewarning i'm extremely new python, , linux based systems in general, if im doing terribly wrong, please feel free redirect me, seemed fastest way there.
here code stands:
import subprocess import os import signal vid1 = ['omxplayer', '--loop', '/home/pi/vids/2779832.mp4'] while true: #vid = subprocess.popen(['omxplayer', '--loop', '/home/pi/vids/2779832.mp4'], stdout=subprocess.pipe, shell=true, preexec_fn=os.setsid) vid = subprocess.popen(vid1, stdout=subprocess.pipe, preexec_fn=os.setsid) print 'sid is: ', preexec_fn #vid = subprocess.popen(['omxplayer', '--loop', '/home/pi/vids/2779832.mp4']) id = raw_input() if not id: break os.killpg(vid.pid, signal.sigterm) print "your input: ", id print "while loop has exited"
so trying write python app raspberry pi plays video on loop (i came across popen way accomplish using omxplayer) , on keyboard interrupt, kills process , opens process (playing different video).
by default, sigint
propagated processes in foreground process group, see "how ctrl+c works". preexec_fn=os.setsid
(or os.setpgrp
) prevents it: use if not want omxplayer
receive ctrl+c i.e., use if manually call os.killpg
when need kill process tree (assuming omxplayer
children not change process group).
"keyboard interrupt" (sigint signal) visible keyboardinterrupt
exception in python. code should catch it:
#!/usr/bin/env python subprocess import call, check_call try: rc = call(['omxplayer', 'first file']) except keyboardinterrupt: check_call(['omxplayer', 'second file']) else: if rc != 0: raise runtimeerror('omxplayer failed play first file, ' 'return code: %d' % rc)
the above assumes omxplayer
exits on ctrl+c
.
you see message due several reasons e.g., omxplayer
not support --loop
option (run manually check) or mistakenly use shell=true
and pass command list: pass command single string if need shell=true
, in reverse: (on posix) pass command list of arguments if shell=false
(default).
Comments
Post a Comment