Is it safe to disconnect a slot before it was call

2019-08-06 07:18发布

问题:

I have several objects which need to be updated. The updating is done in a separate thread and the results will be transferred to the objects via a pyqtSignal. So the updating method loops over all objects like this:

The object class:

class MyObject(QObject):
    def __init__(self, id):
        QObject.__init__(self)

        self.id = id

    def catch_result(self, id, res):
        if id == self.id:
            # do something with res

And the worker class:

class Worker(QObject):

    result = pyqtSignal('QString', 'QString')

    def __init__(self):
        QObject.__init__(self)

        self.objects = []
        # create some objects here

    def update_one(self, obj):
        # do some computation -> will produce res
        self.result.emit(obj.id, res)

    def update_all(self):
        for obj in objects:
            self.result.connect(obj.catch_result)
            self.update_one(obj)
            self.result.disconnect(obj.catch_result)  # is this safe?

The moment the slot catch_result will be disconnected from the signal result it is already emitted. From what I understood so far, after emitting a signal it will be placed in the receivers queue and accordingly processed. However assume that the signal will be disconnected before the slot was called, will the slot still be executed correctly? Is it save to disconnect slots before they have been called but after the corresponding signal was emitted?