python - pyqt's signals vs directly calling a method -
i'm trying come grips why 1 want use pyqt signal/slot instead of calling class method directly.
here simple snippet of code illustrates how see it:
from pyqt4 import qtgui, qtcore class simple_signal(qtcore.qobject): single_signal = qtcore.pyqtsignal() double_signal = qtcore.pyqtsignal() def __init__(self): super().__init__() # connect 1 signal 1 slot self.single_signal.connect(self.handle_signal_choice1) # connect signal 2 slots self.double_signal.connect(self.handle_signal_choice1) self.double_signal.connect(self.handle_signal_choice2) # connect 1 method self.method = self.handle_signal_choice3 def handle_signal_choice1(self): print("simple_signal - choice1 received") def handle_signal_choice2(self): print("simple_signal - choice2 received") def handle_signal_choice3(self): print("simple_signal - choice3 received") if __name__ == "__main__": simple_signal = simple_signal() print('calling choice1 via single_signal') simple_signal.single_signal.emit() print('calling choice1 , choice2 via double_signal') simple_signal.double_signal.emit() print('calling choice3 via class method') simple_signal.method() one immediate advantage signals seems be: it's easy attach signal 2 slots. however, method example without fuss.
the method example seems better cpu time results (ie faster) signal/slot example, advantage may lost if needed many slots called 1 signal.
in mind, calling method cleaner messing signal/slot setup.
either work in qwidgets.
do need more complex situation signals/slots better choice directly calling method?
thanks.
edit: being curious time consumed in each method, did timeit checks.
add these timeit lines code, , replace each 'print' line in original code (ie in handle_signal_choice1) 'pass' printing shell doesn't slow things down.
result=timeit.timeit('simple_signal.single_signal.emit()', 'from __main__ import simple_signal', number=10000) print('calling choice1 via single_signal') print('timeit says : ', result) result=timeit.timeit('simple_signal.double_signal.emit()', 'from __main__ import simple_signal', number=10000) print('calling choice1 , choice2 via double_signal') print('timeit says : ', result) result=timeit.timeit('simple_signal.method()', 'from __main__ import simple_signal', number=10000) print('calling choice3 via method') print('timeit says : ', result) this gives me following results, upholds suspicion single method call faster single signal/slot:
now start timeit tests calling choice1 via single_signal timeit says : 0.010595089312688225 calling choice1 , choice2 via double_signal timeit says : 0.014604222516910098 calling choice3 via method timeit says : 0.0016033988748859057 that's quite big difference if drive socket event loop via signals/slots instead of methods. button clicking shouldn't matter.
the reason use signals , slots isn't speed, it's loose coupling. using signals , slots allows better reuse. there's nice paper goes on motivation , benefits. add indirection, it's worth it, imho.
Comments
Post a Comment