From d0ad409e0f0db6cb879b4e21c9bef708f55ff020 Mon Sep 17 00:00:00 2001 From: Nyre221 <104171042+Nyre221@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:39:38 +0200 Subject: [PATCH] the subprocess is now closed correctly. --- page_viewer.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/page_viewer.py b/page_viewer.py index 4c583ac..f2b29dd 100644 --- a/page_viewer.py +++ b/page_viewer.py @@ -112,8 +112,6 @@ def load_file(self, path, extension): self.qpage.clear() # It is used to kill the process if the viewer has been hidden. self.is_active_viewer = True - # kills the process group if the old process used for pdf conversion is still running. - self.__kill_conversion_process__() # load and set the display mode based on the file. if extension == ".pdf": @@ -162,7 +160,7 @@ def __convert_document__(self, path): def __convert_document_thread__(self, path, temp_dir): # kills the process group if the old process is still running. - self.__kill_conversion_process__() + self.__kill_conversion_process__(self.conversion_process) # libreoffice shell command commands = f"{self.libreoffice_command} --headless --nolockcheck --norestore --convert-to pdf '{path}' --outdir {temp_dir} " @@ -208,20 +206,25 @@ def __convert_document_thread__(self, path, temp_dir): # shows an error if for some reason libreoffice fails to give a path. # True:path error self.signLoadingArrested.emit(False, False, True, "") + # kills the process group + self.__kill_conversion_process__(local_process) return # send self.signConversionFinished.emit(pdf_path) + # kills the process group + self.__kill_conversion_process__(local_process) except IndexError: print("IndexError") process_error = local_process.communicate()[1] # shows an error if for some reason libreoffice fails to give a path. self.signLoadingArrested.emit( False, False, False, process_error.decode("utf-8")) + # kills the process group + self.__kill_conversion_process__(local_process) return - # kills the process group - self.__kill_conversion_process__() + def __conversion_finished__(self, path: str): # calls the function with the path of the new pdf @@ -268,15 +271,16 @@ def __set_placeholder_text__(self): self.label_message_name.setText("Name") self.label_message.setText("") - def __kill_conversion_process__(self): + def __kill_conversion_process__(self,process): # terminate the process used for PDF conversion if it is alive. - if self.conversion_process is not None and self.conversion_process.poll() is None: - os.killpg(self.conversion_process.pid, signal.SIGTERM) + if process is not None and process.poll() is None: + os.killpg(process.pid, signal.SIGTERM) def __app_is_closing__(self): + print("closing pageviewer") # closes the libreoffice process if it is still active. # This is because the process may remain active and slow down quickview the next time it is started. - self.__kill_conversion_process__(self) + self.__kill_conversion_process__(self.conversion_process) if __name__ == '__main__': app = QApplication(sys.argv) widget = PageViewer()