简体   繁体   中英

Signals python threads asynchronous

I have a main thread, which I want non-blocking, (it includes a PyQt5 GUI, I can not use join)

From this thread, I spawn a child thread performing an action.

I want to start an other action from the main thread when the child thread is finished.

I guess I have to generate a signal form the child when it is done and catch it in the main. Is there a snippet showing the proper way to implement this?

There is already a built-in signal called finished, so you can connect it to your process method:

class NewThread(QtCore.QThread):
    def run(self):
        pass

class MainWidget(QtWidgets.QWidget):
    def after_thread_done(self):
        pass

    def __init__(self):
        self.thread = NewThread()
        self.thread.finished.connect(self.after_thread_done)

If you want to emit signal by hand, you can define a new signal:

class NewThread(QtCore.QThread):
    new_signal = QtCore.pyqtSignal()

    def run(self):
        ...
        self.new_signal.emit()
        ...

Then connect it to your process method through the same codes.

If you want to define signals in MainWidget, you can pass main_widget as thread's parent.

class NewThread(QtCore.QThread):
    def run(self):
        ...
        self.parent().signal.emit()
        ...

class MainWidget(QtWidgets.QWidget):
    new_signal = QtCore.pyqtSignal()

    def after_thread_done(self):
        print("done")

    def __init__(self):
        self.new_signal.connect(self.after_thread_done)
        self.thread = NewThread(self)
        self.thread.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM