1

i'm redesigning my software because the last one i did crashed due to a wrong access to a Qt interface by a process started with

serialThread = threading.Thread(target=serialCycle)
serialThread.setDaemon(True)
serialThread.start()

serialThread mainly wait for incoming serial data, decode them and place them in a list (probably i'll move to a numpy array). Informations about connection (serial port, speed, how many data have been received) have to be written in the qt ui. I mainly use global variables to exchange informations between main thread and the serial one; i can ensure that each variable is written only by one thread and read from the other to avoid problems.

The Qt updating is a bit hard to do sending informations to main thread so i looked for another solution. I found this thread ( Updating GUI elements in MultiThreaded PyQT ), but i didn't get the point. If i start a thread with slot and signal can't I have crashes due to multiple access to the same variable?

  • (Question1) started thread runs in parallel and so they shouldn't change the qt interface...

  • (Question2) Can't an entire .ui window be loaded "linked" to a different thread so this thread can update it (and obviously not the main thread)

  • (Question3) Which one is the simpliest way to have a gui that can be updated by different thread about the self status (and to let the user to change parameters)?

Thanks

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
brazoayeye
  • 329
  • 1
  • 2
  • 14
  • 1
    If you are using Qt (pyqt, pyside, etc) I recommend using QThread, it is more friendly with Qt than threading, since this class inherits from QObject and handles the signals and slots, whereas threading does not do so for what you will have to create A class that inherits from QObject and this is the function that uses threading. – eyllanesc Aug 31 '17 at 15:44
  • 1
    As mentioned in your link, it's a bad idea to change GUI from different thread. You should connect worker thread to gui (main) thread with signal-slot mechanism. As @eyllanesc said, QThread is a way to go. Alternatively you can use plain python threads, connected to *signal dispatcher* via [Queues](https://docs.python.org/3.6/library/queue.html#module-Queue). Python thread will put status update to a queue and dispatcher will get that updates and send them as Qt signals. – 9dogs Aug 31 '17 at 20:09
  • I would suggest asking a new question with a specific example of what you are trying to do. There are several approaches to being thread safe (and many more that are not) and it's hard to answer your questions without being overly general. – three_pineapples Sep 03 '17 at 00:30
  • Why not sockets? simple zmq PUSH/PULL/SUB requires ~~ 10 lines for client side, et same for server. They run in different processes and are completly thread safe – dgan Sep 04 '17 at 15:00

0 Answers0