pythonlinuxraspberry-piomxplayer

PID number mismatch


I spawn a process in Python with the following code (just the relevant code):

...
_LAUNCH_CMD = '/usr/bin/omxplayer -s --vol %s %s %s'
...
cmd = self._LAUNCH_CMD % (volume, args, file)
...
self._process = pexpect.spawn(cmd, timeout=5)

It always starts properly. In some cases I need to get the PID of the process and kill it from python code. I tried

self._process.pid

It gives me a PID, but after some time I realized that it did not work, because this command always returns a ten less number than the actual process. For example, when the omxplayer runs and I check the PID with the previous command it returns "24178", but when I use "pidof omxplayer.bin" it returns "24188". I do not know that the 10 is constant or it can change so I do not think it is a good idea to hard code it like

os.system("kill -9 %s" % (self._process.pid + 10))

Anybody knows what causes the difference or what can be the solution?


Solution

  • Probably /usr/bin/omxplayer is a wrapper that executes omxplayer.bin, which gets assigned a new PID. If you execute omxplayer.bin directly, you'll avoid the issue at hand, but you'll need to do the work /usr/bin/omxplayer is doing before launching the real binary. PIDs are random, so you can't bet that it will always be a difference of 10 in the PIDs.