User Tools

Site Tools


module:python:checkthread:checkthread

This is an old revision of the document!


Table of Contents

CheckThread

Benutzung

Python-Library Thread.py :

#
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():
    #
    def __init__(self, onstart, onbusy, onabort, onend):
        self.State = EStateThread.stIdle
        self.Thread = THR.Thread(target=CBOnExecute, args=(self,))
        self.OnStart = onstart
        self.OnBusy = onbusy
        self.OnAbort = onabort
        self.OnEnd = onend
    #
    def Start(self):
        self.State = EStateThread.stBusy
        if (None != self.OnStart):
            self.OnStart(self)
        self.Thread.start()
        return   
    #
    def Abort(self):
        self.State = EStateThread.stEnd
        if (None != self.OnAbort):
            self.OnAbort(self)
        return

Wichtig: Die Library Thread.py muss im FileSystem des MicroControllers vorhanden sein!

Aufrufendes Programm Esp32CheckThreading.py :

#
import time
#
import Thread
#
#-----------------------------------------
def CBOnExecute(thread):
  print('CBOnExecute[{}]: begin'.format(thread.ID))
  I = 0
  while (thread.IsBusy):
    I += 1
    print("CBOnExecute[{}]: Hello({})!".format(thread.ID, I))
    time.sleep(1)
  print('CBOnExecute[{}]: end'.format(thread.ID))
#
#-----------------------------------------
if ('__main__' == __name__):
  print('*** Esp32CheckThreading: begin')
  T = Thread.CThread('T1', CBOnExecute)
  T.Start()
  time.sleep(5)
  T.Abort()
  time.sleep(5)
  print('*** Esp32CheckThreading: end')
#

Run-Ausgabe im Terminal:

>>> Running ..\Esp32Threading.py
>>>
>>> *** Esp32CheckThreading: begin
CBOnExecute[T1]: begin
CBOnExecute[T1]: Hello(1)!
CBOnExecute[T1]: Hello(2)!
CBOnExecute[T1]: Hello(3)!
CBOnExecute[T1]: Hello(4)!
CBOnExecute[T1]: Hello(5)!
CBOnExecute[T1]: end
*** Esp32CheckThreading: end
♦♦>
MicroPython v1.17 on 2021-09-02; ESP32 module with ESP32
Type "help()" for more information.
>>>

Version

module/python/checkthread/checkthread.1639079204.txt.gz · Last modified: 2021/12/09 21:46 (external edit)