- Reordered imports

- Updated comments for multi-thread queue processing
This commit is contained in:
Ross Stewart
2025-08-30 13:48:55 +01:00
parent ce576019c3
commit 15fbdfdeec

View File

@@ -1,11 +1,10 @@
from flask import Flask, render_template from flask import Flask, render_template
import logging import logging
from multiprocessing import Process, Manager
from multiprocessing.managers import BaseManager
import os import os
import random import random
import sys import sys
from multiprocessing import Process, Manager
from multiprocessing.managers import BaseManager
from ask_sdk_core.skill_builder import SkillBuilder from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import AbstractRequestHandler, AbstractRequestInterceptor, AbstractResponseInterceptor from ask_sdk_core.dispatch_components import AbstractRequestHandler, AbstractRequestInterceptor, AbstractResponseInterceptor
@@ -175,13 +174,17 @@ if 'NAVI_DEBUG' in os.environ:
logger.setLevel(logging.WARNING) logger.setLevel(logging.WARNING)
logger.warning('Log level set to WARNING') logger.warning('Log level set to WARNING')
# Create a queue # Create a sharable queue than can be updated by multiple threads to enable larger playlists
# to be returned in the back ground avoiding the Amazon 8 second timeout
BaseManager.register('MediaQueue', queue.MediaQueue) BaseManager.register('MediaQueue', queue.MediaQueue)
manager = BaseManager() manager = BaseManager()
manager.start() manager.start()
play_queue = manager.MediaQueue() play_queue = manager.MediaQueue()
logger.debug('MediaQueue object created...') logger.debug('MediaQueue object created...')
# Variable to store the additional thread used to populate large playlists
# this is used to avoid concurency issues if there is an attempt to load multiple playlists
# at the same time.
backgroundProcess = None backgroundProcess = None
# Connect to Navidrome # Connect to Navidrome
@@ -477,6 +480,8 @@ class NaviSonicPlayPlaylist(AbstractRequestHandler):
global backgroundProcess global backgroundProcess
logger.debug('In NaviSonicPlayPlaylist') logger.debug('In NaviSonicPlayPlaylist')
# Check if a background process is already running, if it is then terminate the process
# in favour of the new process.
if backgroundProcess != None: if backgroundProcess != None:
backgroundProcess.terminate() backgroundProcess.terminate()
backgroundProcess.join() backgroundProcess.join()
@@ -496,9 +501,11 @@ class NaviSonicPlayPlaylist(AbstractRequestHandler):
else: else:
song_id_list = connection.build_song_list_from_playlist(playlist_id) song_id_list = connection.build_song_list_from_playlist(playlist_id)
play_queue.clear() play_queue.clear()
controller.enqueue_songs(connection, play_queue, [song_id_list[0], song_id_list[1]])
backgroundProcess = Process(target=queueWorkerThread, args=(connection, play_queue, song_id_list[2:])) # Work around the Amazon / Alexa 8 second timeout.
backgroundProcess.start() controller.enqueue_songs(connection, play_queue, [song_id_list[0], song_id_list[1]]) # When generating the playlist return the first two tracks.
backgroundProcess = Process(target=queueWorkerThread, args=(connection, play_queue, song_id_list[2:])) # Create a thread to enqueue the remaining tracks
backgroundProcess.start() # Start the additional thread
speech = 'Playing playlist ' + str(playlist.value) speech = 'Playing playlist ' + str(playlist.value)
logger.info(speech) logger.info(speech)
card = {'title': 'AskNavidrome', card = {'title': 'AskNavidrome',