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. PyQt Data sharing between threads

PyQt Data sharing between threads

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.4k Views 3 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.
  • T Offline
    T Offline
    Thombou
    wrote on last edited by
    #1

    Hi all,
    This is my first attempt of multithreading and this question is more about the organisation of the threads.
    I have a GUi where some values are set. I also create some specific objects related to motion stages. The GUI is used to enter some values that will be used in a script. This script takes hours to be run. During the execution of the long script, I would like to display the progress in the main window (progress bar and some text area (=log).

    Therefore I need to run the long script in a separate thread. In this thread, I would define the long script as a function.

    My question is : How to access all data entered in the main window and device handles so that they remain accessible in the separate thread.

    Is the best way to do it the following :

    • Create my objects (motion stages handles etc) in a worker object
    • Use moveToThread() function to move the worker to a separate thread (where the runner is defined)
    • Update those objects with signals/slots with the values set in the GUI?

    This boils down to a more general question:
    Is it good to have objects living in separate threads?

    Thanks for your advice
    Thombou

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

      Hi,

      Since you are using Python, you might also want to check its facilities for handling multiple processes. Not knowing what your other script does I can't really comment on the best solution. It's not unusual to transfer data back and forth between multiple process/thread. The question is usually what it the best way to do that. How does your script generate the information that you want to process in your GUI ?

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

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Since you are using Python, you might also want to check its facilities for handling multiple processes. Not knowing what your other script does I can't really comment on the best solution. It's not unusual to transfer data back and forth between multiple process/thread. The question is usually what it the best way to do that. How does your script generate the information that you want to process in your GUI ?

        T Offline
        T Offline
        Thombou
        wrote on last edited by Thombou
        #3

        Hi,
        Thanks @SGaist

        Basically what I call a script is a python function that takes time to execute.

        The overall picture is that I want to automate some measurements in a lab. Therefore all my mechanical stages and instruments are controlled by python and each of them is a python object, in which I have methods to move the stages, read their positions etc.
        My gui will be use to change some attributes of those objects (initial positions, range of some vectors etc). The measurement function needs to access those parameters.
        Once the measurement started, the parameters won't change.

        My idea was to put all those objects related to the stages in a separate thread and update their attributes with signals slots from the GUI thread.

        Is it the best way to tackle the issue? How to do it in an efficient manner in python?

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          @Thombou said in PyQt Data sharing between threads:

          My idea was to put all those objects related to the stages in a separate thread and update their attributes with signals slots from the GUI thread.
          Is it the best way to tackle the issue? How to do it in an efficient manner in python?

          Based on the sole merit of the above statement, yes. Background processing of "blocking" tasks (that are not event driven), is the common mechanism for doing what you seem to be trying to do. Just make sure that the background threads themselves are not trying to interact directly with the GUI thread and that you've well defined the signal/slot interace to manage them. Also note that mutli-threading in python is haphazard because of something calls the GIL (global interpretter lock) so you may not get true multi-threaded performance even though your programming model is multi-threaded. I often prefer mutli-processing for concurrent python projects due to that constraint, but it does increase the complexity ofthe IPC.

          If you meet the AI on the road, kill it.

          1 Reply Last reply
          3
          • T Offline
            T Offline
            Thombou
            wrote on last edited by
            #5

            Hi thanks for your advice.
            I did that and it seems to works well enough.
            I have basically an object for which I execute the slots in a separate thread using the moveToThread function.
            All interaction with the main window is then done using signals/slots.

            The question is solved, thanks everyone :)

            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