Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved How to 'kill' a QThreadPool process?

    Qt for Python
    pyside python qt for python
    2
    5
    770
    Loading More Posts
    • 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
      ccortez last edited by

      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?

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @ccortez last edited by

        @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 Reply Quote 3
        • C
          ccortez @jsulm last edited by

          @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

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @ccortez last edited by

            @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 Reply Quote 1
            • C
              ccortez @jsulm last edited by

              @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 Reply Quote 0
              • First post
                Last post