[Tutor] subprocess.Popen()

Dave Hill dave at the-hills.org.uk
Sun Dec 2 05:29:43 EST 2018


I am a volunteer at a Heritage Railway in N.Wales and, amongst other 
things, I provide electronics and software for various exhibits in the 
museum. I use the Raspberry Pi to provide various video presentations, 
employing the omxplayer.

I am in the process of updating an application known as the 'Runaway 
Train', originally written some 4ish years ago, as it has started to 
exhibit intermittent faults. This involves playing a video (at 8 times 
normal speed), via two TV screens, of a narrow gauge train ride from the 
view of the locomotive driver, on request, with the visitor located in a 
replica footplate, in which the floor vibrates and smoke emanates.

As I learn more Python I wanted to improve the interaction with 
omxplayer. In the original application I called omxplayer using Popen

__________________________________________________________________________________

     omxp = Popen(['omxplayer', MOVIE_PATH])

followed by

     # wait for video process to finish
     omxp.wait()

__________________________________________________________________________________

Having 'graduated' to Python 3.7, I thought I would explore 
subprocess.Popen, and put the code in a Class, see code below. The video 
runs, but an error occurs, which I do not understand, see further below

__________________________________________________________________________________

# Import subprocess
import subprocess

from io import StringIO

class Player:

     def __init__(self, path, timeout ):

         self.path = path
         self.timeout = timeout

     def playVideo(self, filename, audio):

         MOVIE_PATH = self.path + filename

         if ( audio == "HDMI" ):
             omxp = subprocess.Popen(['omxplayer', '-s', '-o', 'hdmi',\
MOVIE_PATH],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
         else:
             # Call omxplayer - audio to Analog Port
             omxp = subprocess.Popen(['omxplayer', '-s', '-o', 'local',\
MOVIE_PATH],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

         try:
             out = omxp.communicate(timeout=self.timeout)
             print("Try outs =  ", StringIO(out))
         except:
             omxp.kill()
             out = omxp.communicate()
             print("Except outs =  ", StringIO(out))

__________________________________________________________________________________

Traceback (most recent call last):
   File "/home/pi/Code/TestVideo#4.py", line 31, in <module>
     player.playVideo(FILE, 'HDMI')
   File "/home/pi/Code/VideoPlayer.py", line 51, in playVideo
     out = omxp.communicate()
   File "/usr/lib/python3.5/subprocess.py", line 801, in communicate
     stdout, stderr = self._communicate(input, endtime, timeout)
   File "/usr/lib/python3.5/subprocess.py", line 1437, in _communicate
     selector.register(self.stdout, selectors.EVENT_READ)
   File "/usr/lib/python3.5/selectors.py", line 351, in register
     key = super().register(fileobj, events, data)
   File "/usr/lib/python3.5/selectors.py", line 237, in register
     key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
   File "/usr/lib/python3.5/selectors.py", line 224, in _fileobj_lookup
     return _fileobj_to_fd(fileobj)
   File "/usr/lib/python3.5/selectors.py", line 39, in _fileobj_to_fd
     "{!r}".format(fileobj)) from None
ValueError: Invalid file object: <_io.BufferedReader name=8>

__________________________________________________________________________________

 From previous discussions, where do I sit in the demographics! I 
graduated from a degree, that was 95% physics with a small amount of 
electronics, spent 30 years as essentially an electronics engineer, 
during which time I wrote software using Fortran, Basic, Pascal (HP) and 
C++, typically for embedded applications. I have been retired for 10 
years, and occasionally play trains.



More information about the Tutor mailing list