Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Running threads with PyQt

Running threads with PyQt

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 1.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    abdelmoumene
    wrote on last edited by
    #1

    Hi,

    I am developing a PyQt application which requires running threads, I would like to know how to run a function in a Worker and at the same time update a progress bar.

    The function runs from another python file, i did my import normally. But the function is not recognized when I put it at the run section of my worker

    Please find the code below :

    class WorkerT(QObject):
        finished = pyqtSignal()
        progress = pyqtSignal(int)
    
        def run(self):
            func_train()
            while True:
                i = 1
                self.progress.emit(i)
                time.sleep(1)
            self.finished.emit()
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you mean "func_train" ?

      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
      • A Offline
        A Offline
        abdelmoumene
        wrote on last edited by abdelmoumene
        #3

        Yes, the function is called "func_train", I imported it from a second python file. I would like to execute it at run section and update a progress bar at the same time to show the progress of the execution of this function.

        1 Reply Last reply
        0
        • A abdelmoumene

          Hi,

          I am developing a PyQt application which requires running threads, I would like to know how to run a function in a Worker and at the same time update a progress bar.

          The function runs from another python file, i did my import normally. But the function is not recognized when I put it at the run section of my worker

          Please find the code below :

          class WorkerT(QObject):
              finished = pyqtSignal()
              progress = pyqtSignal(int)
          
              def run(self):
                  func_train()
                  while True:
                      i = 1
                      self.progress.emit(i)
                      time.sleep(1)
                  self.finished.emit()
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @abdelmoumene said in Running threads with PyQt:

          But the function is not recognized

          What exactly do you mean by this? Do you get an error message? When it runs the line or when it reads the file & imports? Please paste any error message.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            abdelmoumene
            wrote on last edited by
            #5

            I have this error, when I try to debug my application although I have imported the "load.py" file:

            (<class 'AttributeError'>, AttributeError ("module 'load' has no attribute 'func_load'"), <traceback object at 0x0000023CA2233B48>)

            JonBJ 1 Reply Last reply
            0
            • A abdelmoumene

              I have this error, when I try to debug my application although I have imported the "load.py" file:

              (<class 'AttributeError'>, AttributeError ("module 'load' has no attribute 'func_load'"), <traceback object at 0x0000023CA2233B48>)

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

              @abdelmoumene
              I don't see why the error message states func_load when you say your code has func_train(). I don't know whether putting it inside run() is relevant.

              A 1 Reply Last reply
              1
              • A Offline
                A Offline
                abdelmoumene
                wrote on last edited by
                #7

                I would also like to know if the function "func_train" should be executed inside the while to update the progress bar (the percentage) at the same time or do I have to do this differently?

                1 Reply Last reply
                0
                • JonBJ JonB

                  @abdelmoumene
                  I don't see why the error message states func_load when you say your code has func_train(). I don't know whether putting it inside run() is relevant.

                  A Offline
                  A Offline
                  abdelmoumene
                  wrote on last edited by
                  #8

                  @JonB This is to execute the function in a thread (to avoid freezing the GUI) and at the same time update the percentage of a progress bar that I created.

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

                    How exactly are you importing that function ?

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

                    A 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      How exactly are you importing that function ?

                      A Offline
                      A Offline
                      abdelmoumene
                      wrote on last edited by
                      #10

                      @SGaist

                      import load
                      from load import *
                      
                      JonBJ 1 Reply Last reply
                      0
                      • A abdelmoumene

                        @SGaist

                        import load
                        from load import *
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @abdelmoumene
                        Why do you effectively import twice? Use one or the other? (And preferably not from load import *.)

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          abdelmoumene
                          wrote on last edited by
                          #12

                          Hi,

                          Can you tell me how to run the function and update the progress bar at the same time?

                          Thank you.

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

                            Well, if your function is blocking for a long time then you won't be able to update the progress bar the way you want.

                            It's up to you to implement it in such a way that it reports progress that can then be used to update the progress bar.

                            So what does it do ?

                            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
                            • A Offline
                              A Offline
                              abdelmoumene
                              wrote on last edited by
                              #14

                              @SGaist I would like to update the progress bar during the execution of this task so that it shows the execution progress, do you have a solution for me?

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

                                Not knowing what this task does nor how it is implemented I can only suggest the same thing as my previous answer.

                                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
                                1
                                • dimkD Offline
                                  dimkD Offline
                                  dimk
                                  wrote on last edited by
                                  #16

                                  Here is an example of QThread and progressBar.
                                  https://stackoverflow.com/questions/68189289/pyqt5-qthread-process-finished-with-exit-code-139-interrupted-by-signal-11-sig/68195024#68195024

                                  1 Reply Last reply
                                  1

                                  • Login

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