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. Pyqt UI non responsive on Multithreading with python
Forum Updated to NodeBB v4.3 + New Features

Pyqt UI non responsive on Multithreading with python

Scheduled Pinned Locked Moved Unsolved Qt for Python
15 Posts 3 Posters 3.1k 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 Albitroz

    @Albitroz said in Pyqt UI non responsive on Multi threading with python:

    Hi thanks for the solution, it worked.
    Qt signals & slots., i was planing to use queue instead, is that a bad idea?

    specifically, to transfer data which is generated by thread

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

    @Albitroz
    You can use a queue, that in itself is fine. You should also be careful to mutex, either code yourself or it must be built into the queue.

    The signal/slot would be needed at least to let the other side know (in your case, the main thread) that new data has been queued. How else would you intend to do that bit?

    A 1 Reply Last reply
    0
    • JonBJ JonB

      @Albitroz
      You can use a queue, that in itself is fine. You should also be careful to mutex, either code yourself or it must be built into the queue.

      The signal/slot would be needed at least to let the other side know (in your case, the main thread) that new data has been queued. How else would you intend to do that bit?

      A Offline
      A Offline
      Albitroz
      wrote on last edited by
      #6

      @JonB i was planing to check the queue size at the other end at a pre defined interval, if queue size is greater than a pre defined size, i just dequeue my queue and get the data. thats my plan is that possible?

      JonBJ 1 Reply Last reply
      0
      • JonBJ JonB

        @Albitroz
        start() calls cfg_read_task(). That calls thread_ai.start() but then immediately thread_ai.join(). Which blocks waiting on the task to complete, and that in turn blocks the UI.

        If you need to use a thread let it run as a thread, not block till it completes.

        i would also like to know, how i can pass the data from AI_DAQ.py to Analog_ui.py

        Use Qt signals & slots.

        A Offline
        A Offline
        Albitroz
        wrote on last edited by
        #7

        @JonB in the same code, im facing issues in stopping my thread,
        im calling a function from my main code

        def stop_task():
            running=False
        

        which will set the running variable to False, and in my thread, im checking for that variable

        def ai_read():
            print("Inside Thread")
            task_in.start()
            while(running):
                    data=task_in.read(number_of_samples_per_channel=100)
                    #print(data)
                    time.sleep(.01)
        

        I have verified the value of running with printf inside the thread, its not getting updated, is there any thread safe variable?

        JonBJ 1 Reply Last reply
        0
        • A Albitroz

          @JonB i was planing to check the queue size at the other end at a pre defined interval, if queue size is greater than a pre defined size, i just dequeue my queue and get the data. thats my plan is that possible?

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

          @Albitroz said in Pyqt UI non responsive on Multithreading with python:

          @JonB i was planing to check the queue size at the other end at a pre defined interval, if queue size is greater than a pre defined size, i just dequeue my queue and get the data. thats my plan is that possible?

          It will work, bit it's not as efficient as it could be.

          To do that, your main thread will have to have that interval timer and check each time. Plus often(?) the queue will not be of that size, and it will be a waste of time. Wouldn't you like to know when the thread actually pushes another item to the queue, and only check then/when the count is reached? That is what having the thread send a signal (when it queues), and the main thread have a slot attached to the signal, would achieve.

          The only time where "timed-polling" is (almost) just as good is if you only want the receiver to pull from the queue at those interval points, not as soon as the data arrives. All in all it's hard to see why signalling would not be a preferable solution.

          Sooner or later you will have to use signals & slots in Qt programming, so why not now?

          A 1 Reply Last reply
          0
          • A Albitroz

            @JonB in the same code, im facing issues in stopping my thread,
            im calling a function from my main code

            def stop_task():
                running=False
            

            which will set the running variable to False, and in my thread, im checking for that variable

            def ai_read():
                print("Inside Thread")
                task_in.start()
                while(running):
                        data=task_in.read(number_of_samples_per_channel=100)
                        #print(data)
                        time.sleep(.01)
            

            I have verified the value of running with printf inside the thread, its not getting updated, is there any thread safe variable?

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

            @Albitroz said in Pyqt UI non responsive on Multithreading with python:

            I have verified the value of running with printf inside the thread, its not getting updated, is there any thread safe variable?

            All your threading stuff is being done with Python threading. So your questions are best asked in a Python forum. I don't know when stop_task() gets called while ai_read() is in a loop.

            Up to you, but I would use Qt signals & slots for everything like this.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Albitroz said in Pyqt UI non responsive on Multithreading with python:

              @JonB i was planing to check the queue size at the other end at a pre defined interval, if queue size is greater than a pre defined size, i just dequeue my queue and get the data. thats my plan is that possible?

              It will work, bit it's not as efficient as it could be.

              To do that, your main thread will have to have that interval timer and check each time. Plus often(?) the queue will not be of that size, and it will be a waste of time. Wouldn't you like to know when the thread actually pushes another item to the queue, and only check then/when the count is reached? That is what having the thread send a signal (when it queues), and the main thread have a slot attached to the signal, would achieve.

              The only time where "timed-polling" is (almost) just as good is if you only want the receiver to pull from the queue at those interval points, not as soon as the data arrives. All in all it's hard to see why signalling would not be a preferable solution.

              Sooner or later you will have to use signals & slots in Qt programming, so why not now?

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

              @JonB ok i will use signals and slots as a callback to get the data, but im facing issues stopping the thread

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

                Hi,

                What issues are you having ?

                Note that depending on what you do in your loop you should put break statement at regular intervals to allow for an easier way out of it.

                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

                  Hi,

                  What issues are you having ?

                  Note that depending on what you do in your loop you should put break statement at regular intervals to allow for an easier way out of it.

                  A Offline
                  A Offline
                  Albitroz
                  wrote on last edited by
                  #12

                  @SGaist HI
                  Currently im facing issues, in transferring data from different threads

                  Im splitting my entire project to different ".py " file for easy debugging, in this case how can i transfer data from one file to other

                  to be pressie,

                  I have my main ".py file which runs the pyqt event loop, i have another file which generates some data through a thread". How can i pass data from this thread to my main python file, can i use signals and slots to transfer data to the main file which holds the UI

                  my main intention is to send data from different threads to the main file which runs QT event and UI so that i can display data from these threads

                  which is better, implementing a queue or signals and slots?

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

                    The files play no role here.

                    If you are using QThread then you can easily use signals and slots to transfert data.

                    Otherwise are you using Python's multithreading or multiprocessing ? If you really want parallel processing with Python, you shall use the later.

                    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

                      The files play no role here.

                      If you are using QThread then you can easily use signals and slots to transfert data.

                      Otherwise are you using Python's multithreading or multiprocessing ? If you really want parallel processing with Python, you shall use the later.

                      A Offline
                      A Offline
                      Albitroz
                      wrote on last edited by
                      #14

                      @SGaist
                      I will be handling around 20 threads of different functionalities, and all are time critical activities, so stack overflow suggests using Multiprocessing library which one do you prefer?

                      if i go with multiprocessing library, how can i transfer data between files , my main action is to display these processed data on UI and thats the reason i use PyQt

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

                        You can check this article. At the there's a link for a mix between PyQt and multiprocessing.

                        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

                        • Login

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