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. PYQT5 QThread: Destroyed while thread is still running

PYQT5 QThread: Destroyed while thread is still running

Scheduled Pinned Locked Moved Solved Qt for Python
22 Posts 3 Posters 6.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.
  • Y Offline
    Y Offline
    Yok0
    wrote on last edited by Yok0
    #1

    Hi,

    I am a totally beginner with Qt / PyQT5 and this is my first gui app.

    I'm facing the Qthread destroyed issue. Basically, I'm trying to launch a shell command when user click on a button and display the result in the UI.
    QProcess handle the command and I wrap it with QThread to make it works async. But the program crash and I get QThread: Destroyed while thread is still running.

    After some tests it seems that is because how my code is split.
    I have my mainWindow.py (converted from .ui file) imported to a mainWindowController.py where I set my btn.clicked.connect(self.handleSubmit). In the file I have a function lile

        def handleSubmit(self):
            testThread()
    

    It calls a testThread class imported from testThread.py where I init the QThread and start it

    worker.py

    class WorkerThread(QtCore.QThread):
        def __init__(self, parent=None):
            super(WorkerThread, self).__init__(parent)
        
        def run(self):
            time.sleep(3)
            print("hello")
    

    testThread.py

    from worker import WorkerThread
    
    class testThread():
        def __init__(self):
            self.worker = WorkerThread()
            self.worker.start()
    

    However if I import my Worker directly in my MainWindowController.py and init then start the Thread from my handleSubmit function, It runs without any issue.

    How is it possible to split the code?

    Thanks!

    JonBJ 1 Reply Last reply
    0
    • Y Yok0

      Hi,

      I am a totally beginner with Qt / PyQT5 and this is my first gui app.

      I'm facing the Qthread destroyed issue. Basically, I'm trying to launch a shell command when user click on a button and display the result in the UI.
      QProcess handle the command and I wrap it with QThread to make it works async. But the program crash and I get QThread: Destroyed while thread is still running.

      After some tests it seems that is because how my code is split.
      I have my mainWindow.py (converted from .ui file) imported to a mainWindowController.py where I set my btn.clicked.connect(self.handleSubmit). In the file I have a function lile

          def handleSubmit(self):
              testThread()
      

      It calls a testThread class imported from testThread.py where I init the QThread and start it

      worker.py

      class WorkerThread(QtCore.QThread):
          def __init__(self, parent=None):
              super(WorkerThread, self).__init__(parent)
          
          def run(self):
              time.sleep(3)
              print("hello")
      

      testThread.py

      from worker import WorkerThread
      
      class testThread():
          def __init__(self):
              self.worker = WorkerThread()
              self.worker.start()
      

      However if I import my Worker directly in my MainWindowController.py and init then start the Thread from my handleSubmit function, It runs without any issue.

      How is it possible to split the code?

      Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Yok0 said in PYQT5 QThread: Destroyed while thread is still running:

      I am a totally beginner with Qt / PyQT5 and this is my first gui app.

      In that case, why are you trying to use QThread at all? :) It's the hardest part, especially with Python, so why go there?

      Y 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Your use case does not require threading at all. QProcess is already asynchronous. You should start with learning the asynchronous nature of Qt before heading to threads.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • JonBJ JonB

          @Yok0 said in PYQT5 QThread: Destroyed while thread is still running:

          I am a totally beginner with Qt / PyQT5 and this is my first gui app.

          In that case, why are you trying to use QThread at all? :) It's the hardest part, especially with Python, so why go there?

          Y Offline
          Y Offline
          Yok0
          wrote on last edited by Yok0
          #4

          @JonB Yeah your right, but at that point I thought I needed it, but in my case it is useless and as @SGaist mentioned it, QProcess is already async. Before posting this message I used to get the error QProcess / Qthread destroyed while... and I did not fully understood that error. After refactoring my code everything works perfectly!

          @SGaist my mistake, indeed QProcess is already async, I guess the issue was how my code was organised.

          JonBJ 1 Reply Last reply
          0
          • Y Yok0

            @JonB Yeah your right, but at that point I thought I needed it, but in my case it is useless and as @SGaist mentioned it, QProcess is already async. Before posting this message I used to get the error QProcess / Qthread destroyed while... and I did not fully understood that error. After refactoring my code everything works perfectly!

            @SGaist my mistake, indeed QProcess is already async, I guess the issue was how my code was organised.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Yok0 said in PYQT5 QThread: Destroyed while thread is still running:

            QProcess / Qthread destroyed while

            You get this when you set off a process, don't wait for it finish, and let the QProcess variable go out of scope. It needs to persist as long as the process is running. So e.g.

            void someFunction()
            {
                QProcess proc;    // local, stack variable
                proc.start(...);    // start process, but it hasn't yet finished
                // exiting here will destroy the `proc` variable
                // since the process is running, you'll get the " QProcess / Qthread destroyed while ..."
            }
            
            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              Yok0
              wrote on last edited by
              #6

              Sorry @JonB and @SGaist but I have another question. I'ts not directly linked with QProcess but it's a general question about packaging. Now that's my app running great I packed it with fbs. With the command "fbs run", my app runs fine so I "fbs freeze" it.
              The app shows up, everything working but when my QProcess get's called nothing happens.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                What are you calling from QProcess ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                Y 1 Reply Last reply
                0
                • SGaistS SGaist

                  What are you calling from QProcess ?

                  Y Offline
                  Y Offline
                  Yok0
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    Yok0
                    wrote on last edited by Yok0
                    #9

                    EDIT : soooo Stupid...
                    @SGaist when you asked what I was calling it gave me an idea. I changed my command with an os based one like "ls -l" and it worked. I thought it was the same with my cli app I was calling. So i just threw it like QProcess.start("someapp arg").
                    To make it work I just had to specify the absolute path like QProcess.start("/usr/local/bin/someapp arg").

                    Sorry for that and @SGaist thanks for your assistance!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Rather than hard coding such a path, you might want to consider modifying the PATH environment variable using QProcessEnvironment so that you don't rely on where it could be exactly and also provide the option for your users to change the path where that application can be found.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Y 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Rather than hard coding such a path, you might want to consider modifying the PATH environment variable using QProcessEnvironment so that you don't rely on where it could be exactly and also provide the option for your users to change the path where that application can be found.

                        Y Offline
                        Y Offline
                        Yok0
                        wrote on last edited by Yok0
                        #11

                        @SGaist sure QProcessEnvironment is a better way.
                        Also, I thought I could find the path programmatically like using shutil.which("app") but when the program is bundled and launched from the myApp.app. shutil.which() can't find program in /usr/local... it only find os based executable like ls.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          How are you bundling it ?
                          Is it a compiled executable ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • Y Offline
                            Y Offline
                            Yok0
                            wrote on last edited by
                            #13

                            I use fbs to package it for macos, it gives me an executable myApp.app

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              I meant the application you execute with QProcess.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • Y Offline
                                Y Offline
                                Yok0
                                wrote on last edited by
                                #15

                                Oh it's youtube-dl and it's work fine like I said if I specify the absolute path of the executable in this case it's /usr/local/bin/...
                                Sorry it's not related to QProcess but in some situations youtube-dl needs ffmpeg so I would check if it's installed on the system. That's why I use shutil.which('ffmpeg').
                                It works great in my virtualenv but after compiled the program, shutil can't find the path of ffmpeg. Somehow it can't find apps in /usr/local/bin even if it is in $PATH. However it will find os based app like "ls", "mkdir"...

                                JonBJ 1 Reply Last reply
                                0
                                • Y Yok0

                                  Oh it's youtube-dl and it's work fine like I said if I specify the absolute path of the executable in this case it's /usr/local/bin/...
                                  Sorry it's not related to QProcess but in some situations youtube-dl needs ffmpeg so I would check if it's installed on the system. That's why I use shutil.which('ffmpeg').
                                  It works great in my virtualenv but after compiled the program, shutil can't find the path of ffmpeg. Somehow it can't find apps in /usr/local/bin even if it is in $PATH. However it will find os based app like "ls", "mkdir"...

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @Yok0
                                  Are you certain /usr/local/bin is on your PATH, really? Things like ls/mkdir are in /bin and/or /usr/bin, which will be on $PATH, maybe /usr/local/bin is not.

                                  Y 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @Yok0
                                    Are you certain /usr/local/bin is on your PATH, really? Things like ls/mkdir are in /bin and/or /usr/bin, which will be on $PATH, maybe /usr/local/bin is not.

                                    Y Offline
                                    Y Offline
                                    Yok0
                                    wrote on last edited by
                                    #17

                                    @JonB Yep! I did checked it.
                                    In my app I tried to displayed my $PATH with os.environ.get('PATH'). When I run it in my virtualenv, it shows me to correct $PATH with /usr/bin, /usr/bin/local/, .cargo/bin so on and so forth BUT after bundling the project in an executable, that same command only returns /usr/bin:/bin:/usr/sbin:/sbin
                                    The weird thing is that the app is run by the same user in venv and as a executable. The $PATH is somehow overridden.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • Y Yok0

                                      @JonB Yep! I did checked it.
                                      In my app I tried to displayed my $PATH with os.environ.get('PATH'). When I run it in my virtualenv, it shows me to correct $PATH with /usr/bin, /usr/bin/local/, .cargo/bin so on and so forth BUT after bundling the project in an executable, that same command only returns /usr/bin:/bin:/usr/sbin:/sbin
                                      The weird thing is that the app is run by the same user in venv and as a executable. The $PATH is somehow overridden.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @Yok0
                                      Yes, virtualenv's job is to set your path. It is not surprising it is different from path outside venv.

                                      So your problem is that the executable you wish to run lives in a directory which is not on your usual path.

                                      Y 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @Yok0
                                        Yes, virtualenv's job is to set your path. It is not surprising it is different from path outside venv.

                                        So your problem is that the executable you wish to run lives in a directory which is not on your usual path.

                                        Y Offline
                                        Y Offline
                                        Yok0
                                        wrote on last edited by
                                        #19

                                        @JonB mhm indeed the working direcory of the executable become /
                                        instead of /Users/foo .. obvious, thanks.

                                        JonBJ 1 Reply Last reply
                                        0
                                        • Y Yok0

                                          @JonB mhm indeed the working direcory of the executable become /
                                          instead of /Users/foo .. obvious, thanks.

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by
                                          #20

                                          @Yok0
                                          You're not executing this as root/su on the installed system, are you?

                                          Y 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