W
W
Warlam2021-07-08 18:54:06
Python
Warlam, 2021-07-08 18:54:06

Multithreading in Python (PyQt5)?

When the button is clicked, the "long-term" run() function is launched, in which I need to edit widgets or take values ​​from them (for example, take text from Qlineedit). I don't understand how it can be done

spoiler
class Worker(QObject):
    finished = pyqtSignal()

    def run(self, AuctionID_raw, Price, data3, AuctionID, place):
        while True:
        login = self.lineEdit.text()
        .
        .
        .

class mywindow(QtWidgets.QMainWindow):

     def btnCkicked_2(self):
        .
        .
        .

        self.thread = QThread()
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.run(AuctionID_raw, Price, data3, AuctionID, place))
        self.worker.finished.connect(self.thread.quit)
        self.worker.finished.connect(self.worker.deleteLater)
        self.thread.finished.connect(self.thread.deleteLater)
        self.thread.start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2021-07-08
@Warlam

All interaction of flows and graphics - to take out in signals.

You must pass to it all the data it needs to work with
- load all the necessary data before starting, if the data has changed and the worker needs to receive it, send it to the worker using a signal. When a worker has done a part, it sends the result in a signal.
The whole Qt runs on signals, in multi-threaded mode there is not much difference, only that only signals need to be used to interact with graphics.
UPD, moveToThread has a feature, all class slots will be executed in a separate thread.
As it stands now, it will be very difficult for you to multithread Qt. Classes, fields, instances, the general structure of the application, the Qt signal / slot system, events, thread options (QThread, QRunnable, QThreadPool) - everything about this should be read and learned in python.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question