diff --git a/mini_pupper_music/mini_pupper_music/music_server.py b/mini_pupper_music/mini_pupper_music/music_server.py index 04b21ef..07caf20 100644 --- a/mini_pupper_music/mini_pupper_music/music_server.py +++ b/mini_pupper_music/mini_pupper_music/music_server.py @@ -19,7 +19,7 @@ from rclpy.node import Node from mini_pupper_interfaces.srv import MusicCommand import pygame -import threading +import concurrent.futures import os from ament_index_python.packages import get_package_share_directory @@ -33,6 +33,7 @@ def __init__(self): self.play_sound_callback ) self.song_pool = {'robot1.mp3', 'robot1.wav'} + self.thread_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1) def play_sound_callback(self, request, response): if request.command == 'play': @@ -55,10 +56,8 @@ def play_sound_file(self, file_name): package_path = get_package_share_directory(package_name) sound_path = os.path.join(package_path, 'resource', file_name) - # Create a new thread for playing the sound - thread = threading.Thread(target=self.play_sound_in_background, args=(sound_path,)) - thread.daemon = True # Set the thread as a daemon (will exit when the main program ends) - thread.start() + # Submit the playback task to the thread pool + self.thread_executor.submit(self.play_sound_in_background, sound_path) def play_sound_in_background(self, sound_path): pygame.init()