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

Next revision
Previous revision
module:micropython:multiprocessexecution:multiprocessexecution01v01 [2021/12/14 14:35] – external edit 127.0.0.1module: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 python>
 +>>>
 +*** Check Esp32MultiProcessExecution: begin
 +CBOnStart[CU]
 +CBOnBusy[CU]
 +*** Check CountUp: begin
 +CountUp---System.Counter[1]
 +CBOnStart[CD]
 +CBOnBusy[CD]
 +*** Check CountDown: begin
 +CountDown-System.Counter[0]
 +CountUp---System.Counter[1]
 +CountUp---System.Counter[2]
 +CountDown-System.Counter[1]
 +CountUp---System.Counter[2]
 +CountDown-System.Counter[1]
 +CountUp---System.Counter[2]
 +CountUp---System.Counter[3]
 +CountDown-System.Counter[2]
 +CountUp---System.Counter[3]
 +CountDown-System.Counter[2]
 +CountUp---System.Counter[3]
 +CountUp---System.Counter[4]
 +CountDown-System.Counter[3]
 +CountUp---System.Counter[4]
 +CountDown-System.Counter[3]
 +*** Check CountUp: end
 +CBOnEnd[CU]
 +CountDown-System.Counter[2]
 +CountDown-System.Counter[1]
 +CountDown-System.Counter[0]
 +*** Check CountDown: end
 +CBOnEnd[CD]
 +CBOnAbort[CU]
 +CBOnAbort[CD]
 +*** Check Esp32MultiProcessExecution: end
 +>>>
 </code> </code>
  
 +Globale System-Ressourcen **System.py**
 +<code python>
 +#
 +# Global - Variable
 +#--------------------------------------------------------------
 +Counter = 0
 +#
 +</code>
  
 Hauptmodul **Esp32MultiProcessExecution.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 **BlinkFast.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 **BlinkSlow.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> </code>
  
-ChildProcess **CountUp.py** +Library **Process.py** 
-<code python>  +<code python> 
-</code> +import time 
- +# 
-ChildProcess **CountDown.py** +import Thread as THR 
-<code python> +
 +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 45: 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.1639488910.txt.gz · Last modified: 2021/12/14 15:35 (external edit)