Skip to content

Commit

Permalink
use thread pool instead of thread
Browse files Browse the repository at this point in the history
  • Loading branch information
CullenSUN committed Nov 12, 2023
1 parent e8805dd commit b96dbb5
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions mini_pupper_music/mini_pupper_music/music_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Check warning on line 36 in mini_pupper_music/mini_pupper_music/music_server.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 trailing whitespace Raw Output: ./mini_pupper_music/mini_pupper_music/music_server.py:36:84: W291 trailing whitespace

def play_sound_callback(self, request, response):
if request.command == 'play':
Expand All @@ -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()
Expand Down

0 comments on commit b96dbb5

Please sign in to comment.