Replace busy-wait loop with threading.Event for better efficiency

Co-authored-by: denizsafak <39929354+denizsafak@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-20 23:13:22 +00:00
co-authored by denizsafak
parent 7f75aa8209
commit 5ec06e7d49
+5 -3
View File
@@ -653,6 +653,7 @@ class ConversionThread(QThread):
): # Add use_gpu parameter ): # Add use_gpu parameter
super().__init__() super().__init__()
self._chapter_options_event = threading.Event() self._chapter_options_event = threading.Event()
self._timestamp_response_event = threading.Event()
self.np = np_module self.np = np_module
self.KPipeline = kpipeline_class self.KPipeline = kpipeline_class
self.file_name = file_name self.file_name = file_name
@@ -872,15 +873,15 @@ class ConversionThread(QThread):
self.log_updated.emit("\nDetected timestamps in text file") self.log_updated.emit("\nDetected timestamps in text file")
# Signal to ask user (-1 indicates timestamp detection) # Signal to ask user (-1 indicates timestamp detection)
self.chapters_detected.emit(-1) self.chapters_detected.emit(-1)
# Wait for user response # Wait for user response using event instead of busy-wait loop
while not hasattr(self, "_timestamp_response"): self._timestamp_response_event.wait()
if self.cancel_requested: if self.cancel_requested:
self.conversion_finished.emit("Cancelled", None) self.conversion_finished.emit("Cancelled", None)
return return
time.sleep(0.1)
if not self._timestamp_response: if not self._timestamp_response:
is_timestamp_text = False is_timestamp_text = False
delattr(self, "_timestamp_response") delattr(self, "_timestamp_response")
self._timestamp_response_event.clear()
# Process subtitle files separately # Process subtitle files separately
if is_subtitle_file or is_timestamp_text: if is_subtitle_file or is_timestamp_text:
@@ -2183,6 +2184,7 @@ class ConversionThread(QThread):
def set_timestamp_response(self, treat_as_subtitle): def set_timestamp_response(self, treat_as_subtitle):
"""Set whether to treat timestamp text file as subtitle.""" """Set whether to treat timestamp text file as subtitle."""
self._timestamp_response = treat_as_subtitle self._timestamp_response = treat_as_subtitle
self._timestamp_response_event.set()
def _extract_and_add_metadata_tags_to_ffmpeg_cmd(self): def _extract_and_add_metadata_tags_to_ffmpeg_cmd(self):
"""Extract metadata tags from text content and add them to ffmpeg command""" """Extract metadata tags from text content and add them to ffmpeg command"""