Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to 'kill' a QThreadPool process?
Forum Updated to NodeBB v4.3 + New Features

How to 'kill' a QThreadPool process?

Scheduled Pinned Locked Moved Unsolved Qt for Python
pysidepythonqt for python
5 Posts 2 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ccortez
    wrote on last edited by
    #1

    I am currently having using the pyside bult in QThreadPool class to launch threads in my application. Upon looking around in the documentation, I am having an issue trying to find a way to 'kill' a process.

    I need to have this capability since one of the threads that I am launching has matplotlib open, and it stays that way after that code runs which messes with the program.

    The solution seems to be that I need to kill that process, but I am not sure how to do this with QThreadPool.

    Does QThreadPool have a method of doing this or Python Qt in general? Or should I attempt to do this another way?

    jsulmJ 1 Reply Last reply
    0
    • C ccortez

      I am currently having using the pyside bult in QThreadPool class to launch threads in my application. Upon looking around in the documentation, I am having an issue trying to find a way to 'kill' a process.

      I need to have this capability since one of the threads that I am launching has matplotlib open, and it stays that way after that code runs which messes with the program.

      The solution seems to be that I need to kill that process, but I am not sure how to do this with QThreadPool.

      Does QThreadPool have a method of doing this or Python Qt in general? Or should I attempt to do this another way?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ccortez You need QThread instance you want to kill then you can call https://doc.qt.io/qt-6/qthread.html#terminate

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      3
      • jsulmJ jsulm

        @ccortez You need QThread instance you want to kill then you can call https://doc.qt.io/qt-6/qthread.html#terminate

        C Offline
        C Offline
        ccortez
        wrote on last edited by
        #3

        @jsulm Thank you! This some what helped, but I am getting a freezing GUI, which I think defeats the point of doing the multithreading. Could you possibly shine light on why this may be happening if you can?

        Here is a sample of my code if it helps:

        class Worker(QObject):
            finished = Signal(str)
            progress = Signal(int)
        
            def run(self, file):
                """Long-running task." that calls a separate class for computation""
                b = SeparateClass()
                b.doComputation()
                
                self.finished.emit()
        class DataPlotting(QMainWindow):
            def __init__(self):
                self.thread = QThread()
                self.worker = Worker()
                self.report_builder = QPushButton('Call class that threads')
                self.report_builder.setEnabled(False)
                self.report_builder.clicked.connect(self.qthread_test)
            @Slot()
            def qthread_test(self):
                file = self.file_dropdown.currentText()
        
                self.worker.moveToThread(self.thread)
                self.thread.started.connect(self.worker.run(file))
                self.worker.finished.connect(self.thread.quit)
                self.worker.finished.connect(self.worker.deleteLater)
                self.thread.finished.connect(self.thread.deleteLater)
                self.thread.start()
                return
        

        This does achieve what I wanted of launching a separate thread and getting rid of it , but the GUI still freezes up

        jsulmJ 1 Reply Last reply
        0
        • C ccortez

          @jsulm Thank you! This some what helped, but I am getting a freezing GUI, which I think defeats the point of doing the multithreading. Could you possibly shine light on why this may be happening if you can?

          Here is a sample of my code if it helps:

          class Worker(QObject):
              finished = Signal(str)
              progress = Signal(int)
          
              def run(self, file):
                  """Long-running task." that calls a separate class for computation""
                  b = SeparateClass()
                  b.doComputation()
                  
                  self.finished.emit()
          class DataPlotting(QMainWindow):
              def __init__(self):
                  self.thread = QThread()
                  self.worker = Worker()
                  self.report_builder = QPushButton('Call class that threads')
                  self.report_builder.setEnabled(False)
                  self.report_builder.clicked.connect(self.qthread_test)
              @Slot()
              def qthread_test(self):
                  file = self.file_dropdown.currentText()
          
                  self.worker.moveToThread(self.thread)
                  self.thread.started.connect(self.worker.run(file))
                  self.worker.finished.connect(self.thread.quit)
                  self.worker.finished.connect(self.worker.deleteLater)
                  self.thread.finished.connect(self.thread.deleteLater)
                  self.thread.start()
                  return
          

          This does achieve what I wanted of launching a separate thread and getting rid of it , but the GUI still freezes up

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ccortez When does your UI freeze?
          The code you posted should not freeze, so it is something different.
          Does it freeze when you try to kill the thread? If so please post the code.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          1
          • jsulmJ jsulm

            @ccortez When does your UI freeze?
            The code you posted should not freeze, so it is something different.
            Does it freeze when you try to kill the thread? If so please post the code.

            C Offline
            C Offline
            ccortez
            wrote on last edited by
            #5

            @jsulm My UI freezes when I press the button that goes to 'qthread_test' in that code snippet.
            It starts to buffer and becomes unresponsive for a bit. That is the code sample that caused it. Yes there rare other elemetns in my code, but they do not connect ao any parts of this, it is mostly other elements like buttons. variables, etc.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved