Fix for crashing process and restarting

This commit is contained in:
Dirk Alders 2024-02-26 19:20:06 +01:00
parent 2adab8e24c
commit 3831e56e3d
2 changed files with 15 additions and 10 deletions

View File

@ -16,7 +16,7 @@ DEVICE_NAME = "Multimedia"
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
APP_NAME = "spotify"
LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout'
LOGLVL = 'DEBUG'
LOGLVL = 'INFO'
LOGHOST = 'cutelog'
LOGPORT = 19996

View File

@ -28,11 +28,7 @@ class librespot(object):
logger.info("Starting Librespot...")
self.__state_callback__ = state_callback
self.__title_callback__ = title_callback
self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
shell=False,
# We pipe the output to an internal pipe
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
self.__start_process__()
self.__state__ = None
self.__preload_state__ = False
self.__title__ = None
@ -41,18 +37,27 @@ class librespot(object):
self.set_state(False)
self.set_title("")
def __start_process__(self):
self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
shell=False,
# We pipe the output to an internal pipe
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
def run(self):
while True:
output = self.__process__.stdout.readline()
# Polling returns None when the program is still running, return_code otherwise
return_code = self.__process__.poll()
if return_code is not None:
self.__process__.close()
#self.__process__.close()
# Program ended, get exit/return code
raise RuntimeError("Command '{}' finished with exit code {}".format(command, return_code))
#raise RuntimeError
logger.exception(("Command '{}' finished with exit code {}".format(command, return_code)))
# If the output is not empty, feed it to the function, strip the newline first
if output:
self.__start_process__()
output = None
elif output:
out_txt = output.decode('utf-8').strip('\n').strip()
out_txt = out_txt[out_txt.find(']') + 2:]
#logger.debug("librespot output: %s", out_txt)