====== Module - Python - TaskEvent ====== [[http://www.openhardsoftware.de/ | Open Hard- & Software]] [ [[http://www.openhardsoftware.de/dokuwiki | DokuWiki]] [[http://www.openhardsoftware.de/websites | WebSites]] [[http://www.openhardsoftware.de/mediawiki | MediaWiki]] [[http://www.openhardsoftware.de/nextcloud | NextCloud]] ] ===== Übersicht ===== TaskAction durchläuft eine Schleife mit COUNT_ACTIONCYCLE-Durchgängen und wartet in jeder Schleife auf die Fertigstellung der Berechnung des Subtask TaskRefresh: * TaskAction: * begin * Beginn Schleife: löscht den EventReady := 0 * starten des Subtask: TaskRefresh * wartet auf das Ergebnis von TaskRefresh (warten auf: EventReady == 1 gesetzt) * TaskRefresh: * begin * (NC löscht den EventReady := 0) * beginnt Berechnung... * setzt EventReady := 1 nach erfolgter Berechnung * end Download aktuelle Version: {{:module:python:taskevent:2210211928_taskevent_01v01.zip | 2210211928_TaskEvent_01V01.zip}} ===== ToDo ===== --- ===== Done ===== ==== 221021 ==== Hauptprogramm: # import time as TIM import threading as THR # import Task as TSK # #--------------------------------------------------------- # Constant #--------------------------------------------------------- COUNT_ACTIONCYCLE = 3 # #--------------------------------------------------------- # Field - Global #--------------------------------------------------------- #--------------------------------------------------------- # Callback - Task - Refresh #--------------------------------------------------------- def OnTaskRefreshStart(task): #print('OnTaskRefresh-Start[{0}]: begin'.format(task.ID)) task.GetArguments()[0].clear() #print('OnTaskRefresh-Start[{0}]: end'.format(task.ID)) return def OnTaskRefreshAbort(task): #print('OnTaskRefresh-Abort[{0}]: begin'.format(task.ID)) task.GetArguments()[0].set() # stop waiting on abort! #print('OnTaskRefresh-Abort[{0}]: end'.format(task.ID)) return def OnTaskRefreshExecute(task): #print('OnTaskRefresh-Execute[{0}]: begin'.format(task.ID)) print('### TaskRefresh - Busy...') TIM.sleep(3.0) print('### TaskRefresh - SetEvent!') task.GetArguments()[0].set() #print('OnTaskRefresh-Execute[{0}]: end'.format(task.ID)) return False #--------------------------------------------------------- # Callback - Task - Action #--------------------------------------------------------- def OnTaskActionStart(task): #print('OnTaskAction-Start[{0}]: begin'.format(task.ID)) #print('OnTaskAction-Start[{0}]: end'.format(task.ID)) return def OnTaskActionAbort(task): #print('OnTaskAction-Abort[{0}]: begin'.format(task.ID)) TaskRefresh.Abort() #print('OnTaskAction-Abort[{0}]: end'.format(task.ID)) return def OnTaskActionExecute(task): print('OnTaskAction-Execute[{0}]<{1}>: begin'.format(task.ID, task.GetCounter())) print('### TaskAction - Start Refresh and Wait for Event...') TaskRefresh.Start() task.GetArguments()[0].wait() print('### TaskAction - Refresh Finished (Event set)') #print('OnTaskAction-Execute[{0}]: end'.format(task.ID)) return (task.GetCounter() < COUNT_ACTIONCYCLE) #--------------------------------------------------------- # Main #--------------------------------------------------------- if ('__main__' == __name__): print('*** MainProgram-TaskEvent: begin') # EventReady = THR.Event() # TaskRefresh = TSK.CTask('TaskRefresh', OnTaskRefreshStart, OnTaskRefreshAbort, OnTaskRefreshExecute, (EventReady,)) TaskAction = TSK.CTask('TaskAction', OnTaskActionStart, OnTaskActionAbort, OnTaskActionExecute, (EventReady,)) TaskAction.Start() # print('*** MainProgram-TaskEvent: end') # # # # Ausgabe der Kommandozeile: runfile('...TaskEvent.py', wdir='...2210211928_TaskEvent_01V01') *** MainProgram-TaskEvent: begin OnTaskAction-Execute[TaskAction]<1>: begin ### TaskAction - Start Refresh and Wait for Event... *** MainProgram-TaskEvent: end ### TaskRefresh - Busy... ### TaskRefresh - SetEvent! ### TaskAction - Refresh Finished (Event set) OnTaskAction-Execute[TaskAction]<2>: begin ### TaskAction - Start Refresh and Wait for Event... ### TaskRefresh - Busy... ### TaskRefresh - SetEvent! ### TaskAction - Refresh Finished (Event set) OnTaskAction-Execute[TaskAction]<3>: begin ### TaskAction - Start Refresh and Wait for Event... ### TaskRefresh - Busy... ### TaskRefresh - SetEvent! ### TaskAction - Refresh Finished (Event set) ==== 221021 ==== * Aufbau der Event-Library, Basis Python: * import threading * e = threading.Event() * Neu: Argument-Übergabe (hier Event) an Threads! * [[https://www.bogotobogo.com/python/Multithread/python_multithreading_Event_Objects_between_Threads.php | MULTITHREADING - EVENT OBJECTS BETWEEN THREADS]] * Verbesserung der CTask-Library mit Argument-Übergabe, Counter, OnExecute -> Result * Neue Klasse: CTask * Direkte Klasse: threading-Event ----- [[http://www.openhardsoftware.de/ | Open Hard- & Software]] [ [[http://www.openhardsoftware.de/dokuwiki | DokuWiki]] [[http://www.openhardsoftware.de/websites | WebSites]] [[http://www.openhardsoftware.de/mediawiki | MediaWiki]] [[http://www.openhardsoftware.de/nextcloud | NextCloud]] ]