PyQt Data sharing between threads
-
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 -
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 ?
-
Hi,
Thanks @SGaistBasically 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?
-
@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.
-
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 :)