User Tools

Site Tools


module:micropython:multiprocessexecution:multiprocessexecution01v01

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:micropython:multiprocessexecution:multiprocessexecution01v01 [2021/12/14 16:12] – [Benutzung] omdevelopmodule:micropython:multiprocessexecution:multiprocessexecution01v01 [2022/09/13 11:58] (current) – external edit 127.0.0.1
Line 10: Line 10:
 ===== Übersicht ===== ===== Übersicht =====
  
 +**CountUp.py** und **CountDown.py** sind zwei Python-Scripts, welche  
 +  * als Dateien im Esp32-MicroPython-FileSystem vorhanden sind und 
 +  * jeweils in einem eigenen Thread PARALLEL ablaufen und 
 +  * dabei auf eine (globale) System-Ressource (Counter in System.py) R/W-zugreifen.
 ===== Benutzung ===== ===== Benutzung =====
 VSCode-MicroPython-Terminal: VSCode-MicroPython-Terminal:
-<code python> 
-</code> 
- 
- 
-Hauptmodul **Esp32MultiProcessExecution.py** 
 <code python> <code python>
 >>> >>>
Line 57: Line 55:
 </code> </code>
  
-ChildProcess **BlinkFast.py** +Globale System-Ressourcen **System.py** 
-<code python> +<code python> 
 +
 +# Global - Variable 
 +#-------------------------------------------------------------- 
 +Counter = 0 
 +#
 </code> </code>
  
-ChildProcess **BlinkSlow.py** +Hauptmodul **Esp32MultiProcessExecution.py** 
-<code python> +<code python> 
 +
 +import time 
 +
 +import Process as PRC 
 +
 +#---------------------------------------------------------------------     
 +#   Main 
 +#---------------------------------------------------------------------     
 +if ('__main__' == __name__): 
 +    # 
 +    print('*** Check Esp32MultiProcessExecution: begin'      
 +    # 
 +    ProcessCU = PRC.CProcess('CU', 'CountUp.py'
 +    ProcessCD = PRC.CProcess('CD', 'CountDown.py'
 +    ProcessCU.Start() 
 +    time.sleep(0.1) 
 +    ProcessCD.Start() 
 +    time.sleep(3.0) 
 +    ProcessCU.Abort() 
 +    ProcessCD.Abort() 
 +    # 
 +    print('*** Check Esp32MultiProcessExecution: end'      
 +#
 </code> </code>
  
 ChildProcess **CountUp.py** ChildProcess **CountUp.py**
 <code python>  <code python> 
 +#
 +import time
 +#
 +import System
 +#   
 +#-----------------------------------------------------------
 +#   Main
 +#-----------------------------------------------------------
 +if ('__main__' == __name__):
 +    #
 +    print('*** Check CountUp: begin')
 +    #
 +    for I in range(0, 10):
 +        System.Counter += 1
 +        print('CountUp---System.Counter[{}]'.format(System.Counter))
 +        time.sleep(0.1)
 +    #
 +    print('*** Check CountUp: end')
 +    #
 +#
 </code> </code>
  
 ChildProcess **CountDown.py** ChildProcess **CountDown.py**
 <code python>  <code python> 
 +#
 +import time
 +#
 +import System
 +#   
 +#-----------------------------------------------------------
 +#   Main
 +#-----------------------------------------------------------
 +if ('__main__' == __name__):
 +    #
 +    print('*** Check CountDown: begin')
 +    #
 +    for I in range(0, 10):
 +        System.Counter -= 1
 +        print('CountDown-System.Counter[{}]'.format(System.Counter))
 +        time.sleep(0.15)
 +    #
 +    print('*** Check CountDown: end')
 +    #
 +#
 +</code>
 +
 +Library **Process.py**
 +<code python>
 +import time
 +#
 +import Thread as THR
 +#
 +class CProcess():
 +    #
 +    def __init__(self, processid, filename):
 +        self.ID = processid
 +        self.Filename = filename
 +        self.Thread = THR.CThread(self.CBOnStart, self.CBOnBusy, \
 +                                  self.CBOnAbort, self.CBOnEnd)
 +    #
 +    def CBOnStart(self, thread):
 +        print('CBOnStart[{}]'.format(self.ID))
 +        return
 +    def CBOnBusy(self, thread):
 +        print('CBOnBusy[{}]'.format(self.ID))
 +        execfile(self.Filename)
 +        return
 +    def CBOnAbort(self, thread):
 +        print('CBOnAbort[{}]'.format(self.ID))
 +        return
 +    def CBOnEnd(self, thread):
 +        print('CBOnEnd[{}]'.format(self.ID))
 +        return
 +    #
 +    def GetID(self):
 +        return self.ID
 +    #
 +    def Start(self):
 +        self.Thread.Start()
 +    #
 +    def Abort(self):
 +        self.Thread.Abort()
 +    #
 +#
 </code> </code>
  
 Library **Thread.py** Library **Thread.py**
 <code python> <code python>
 +import time
 +import _thread
 +#
 +# States
 +stIdle = 0
 +stBusy = 1
 +stEnd = 2
 +#
 +class CThread():
 +    #
 +    def __init__(self, onstart, onbusy, onabort, onend):
 +        self.State = stIdle        
 +        self.Thread = None
 +        self.OnStart = onstart
 +        self.OnBusy = onbusy
 +        self.OnAbort = onabort
 +        self.OnEnd = onend
 +        return
 +    #
 +    def Start(self):
 +        self.State = stBusy
 +        if (None != self.OnStart):
 +            self.OnStart(self)
 +        self.ThreadID = _thread.start_new_thread(self.CBOnExecute, [self])
 +        return    
 +    #
 +    def Abort(self):
 +        self.State = stEnd
 +        if (None != self.OnAbort):
 +            self.OnAbort(self)
 +        return
 +    #
 +    def CBOnExecute(self, thread):
 +        if (None != self.OnBusy):
 +            self.OnBusy(self)
 +        if (None != self.OnEnd):
 +            self.OnEnd(self)
 +        return
 +    #
 +#   
 </code> </code>
  
Line 80: Line 226:
  
 ===== Version ===== ===== Version =====
-{{:module:micropython:MultiProcessExecution:_MultiProcessExecution_01V01.zip|_MultiProcessExecution_01V01.zip}}+{{:module:micropython:MultiProcessExecution:2112141638_MultiProcessExecution_01V01.zip|2112141638_MultiProcessExecution_01V01.zip}}
  
 ----- -----
module/micropython/multiprocessexecution/multiprocessexecution01v01.1639494744.txt.gz · Last modified: 2021/12/14 17:12 (external edit)