User Tools

Site Tools


module:python:checkthread:checkthread

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
module:python:checkthread:checkthread [2021/12/09 20:45] omdevelopmodule:python:checkthread:checkthread [2022/09/13 11:58] (current) – external edit 127.0.0.1
Line 9: Line 9:
  
 ===== Benutzung ===== ===== Benutzung =====
-Beispiel für die Einbindung der MicroPython-Library **Thread.py** :+Python-Library **Thread.py** :
 <code python> <code python>
 # #
-import _thread+import threading as THR 
 +import enum as ENU 
 +
 +class EStateThread(ENU.Enum): 
 +    stIdle = 0 
 +    stBusy = 1 
 +    stEnd = 2 
 +
 +def CBOnExecute(thread): 
 +    if (None != thread.OnBusy): 
 +        thread.OnBusy(thread) 
 +    if (None != thread.OnEnd): 
 +        thread.OnEnd(thread)
 # #
 class CThread(): class CThread():
-  +    
-  def __init__(self, threadidonexecute): +    def __init__(self, onstart, onbusy, onabortonend): 
-    self.ID threadid +        self.State EStateThread.stIdle 
-    self.OnExecute onexecute +        self.Thread THR.Thread(target=CBOnExecute, args=(self,)) 
-    self.IsBusy False +        self.OnStart onstart 
-    return +        self.OnBusy = onbusy 
-  +        self.OnAbort = onabort 
-  def Start(self): +        self.OnEnd = onend 
-    self.IsBusy True +    
-    _thread.start_new_thread(self.OnExecute, [self]+    def Start(self): 
-    return +        self.State EStateThread.stBusy 
-  +        if (None != self.OnStart): 
-  def Abort(self): +            self.OnStart(self) 
-    self.IsBusy False +        self.Thread.start() 
-    return +        return    
-  #+    
 +    def Abort(self): 
 +        self.State EStateThread.stEnd 
 +        if (None != self.OnAbort): 
 +            self.OnAbort(self) 
 +        return
 </code> </code>
-Wichtig: Die Library **Thread.py** muss im FileSystem des MicroControllers vorhanden sein! 
  
-Aufrufendes Programm **Esp32CheckThreading.py** :+ 
 +Aufrufendes Programm **CheckThread.py** :
 <code python> <code python>
 # #
 import time import time
 # #
-import Thread+import Thread as THR
 # #
-#----------------------------------------- +def CBOnStart(thread): 
-def CBOnExecute(thread): +    print('CBOnStart'
-  print('CBOnExecute[{}]: begin'.format(thread.ID)+    return 
-  I = +
-  while (thread.IsBusy): +def CBOnBusy(thread): 
-    I += 1 +    print('CBOnBusy: 0') 
-    print("CBOnExecute[{}]: Hello({})!".format(thread.ID, I)+    while (THR.EStateThread.stBusy == thread.State): 
-    time.sleep(1) +        print('CBOnBusy...'
-  print('CBOnExecute[{}]: end'.format(thread.ID))+        time.sleep(1.0
 +    return 
 +
 +def CBOnAbort(thread): 
 +    print('CBOnAbort'
 +    return 
 +
 +def CBOnEnd(thread)
 +    print('CBOnEnd') 
 +    return
 # #
-#-----------------------------------------+#-----------------------------------------------------------
 if ('__main__' == __name__): if ('__main__' == __name__):
-  print('*** Esp32CheckThreading: begin'+    print('*** CheckThread: begin'
-  T = Thread.CThread('T1'CBOnExecute+    # 
-  T.Start() +    Thread = THR.CThread(CBOnStart, CBOnBusy, CBOnAbortCBOnEnd
-  time.sleep(5) +    Thread.Start() 
-  T.Abort() +    time.sleep(5.0
-  time.sleep(5) +    Thread.Abort() 
-  print('*** Esp32CheckThreading: end')+    # 
 +    print('*** CheckThread: end') 
 +    #
 # #
 </code> </code>
Line 65: Line 93:
 Run-Ausgabe im Terminal: Run-Ausgabe im Terminal:
 <code> <code>
->>> Running ..\Esp32Threading.py +In [19]: runfile('C:/Downloads/python/CheckThread.py', wdir='C:/Downloads/python') 
->>> +Reloaded modules: Thread 
->>> *** Esp32CheckThreading: begin +*** CheckThread: begin 
-CBOnExecute[T1]: begin +CBOnStart 
-CBOnExecute[T1]Hello(1)! +CBOnBusy0 
-CBOnExecute[T1]: Hello(2)! +CBOnBusy... 
-CBOnExecute[T1]: Hello(3)! +CBOnBusy... 
-CBOnExecute[T1]: Hello(4)! +CBOnBusy... 
-CBOnExecute[T1]: Hello(5)! +CBOnBusy... 
-CBOnExecute[T1]: end +CBOnBusy... 
-*** Esp32CheckThreading: end +CBOnAbort 
-♦♦> +*** CheckThread: end 
-MicroPython v1.17 on 2021-09-02; ESP32 module with ESP32 +CBOnEnd 
-Type "help()" for more information. +
->>>+
 </code> </code>
  
Line 86: Line 113:
 ===== Version ===== ===== Version =====
  
-{{:module:micropython:2112081352_Esp32CheckThreading_01V01.zip | 2112081352_Esp32CheckThreading_01V01.zip}}+{{:module:python:CheckThread:2112092053_CheckThread_01V01.zip | 2112092053_CheckThread_01V01.zip}}
  
  
module/python/checkthread/checkthread.1639079137.txt.gz · Last modified: 2021/12/09 21:45 (external edit)