質問
イベントハンドラーがさっぱり分かりません。。。
①self.OnEH += objMeth の行なのですが、
インクリメントは数値や文字列しかできないという考え方だったのですが関数のポインタもできるのでしょうか?
②マルチスレッドを使用した場合に、このイベントハンドラーをどのように使うのか教えてください。func1とfunc2の関数をそれぞれ非同期処理させている場合に、func2でイベントを発生させてそれをfunc1に通知することはできるのでしょうか。
質問内容が抽象的ですいません。お力添えいただけると幸いです。
import time class Eventsample(object): def __init__(self): self.__eventhandlersample = [] def __iadd__(self, Ehandler): self.__eventhandlersample.append(Ehandler) return self def __isub__(self, Ehandler): self.__eventhandlersample.remove(Ehandler) return self def __call__(self, *args, **keywargs): for eventhandlersample in self.__eventhandlersample: eventhandlersample(*args, **keywargs) class MessToDisplay(object): def __init__(self, Mess): self._tele = Mess def PrintM(self): print("start") for i in range(1,10): print(str(i)+'sec') time.sleep(1) print('end') class sampleclass(object) : def __init__(self) : self.OnEH = Eventsample() def Ehnew(self) : self.OnEH() def anotherevent(self,objMeth): self.OnEH += objMeth def nexteven(self,objMeth) : self.OnEH -= objMeth def Simulation() : newsample = sampleclass() displayamess = MessToDisplay(100) newsample.anotherevent(displayamess.PrintM) newsample.Ehnew() if __name__ == "__main__": Simulation()
補足情報(FW/ツールのバージョンなど)
Python 3.10
Windows 10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。