User Tools

Site Tools


module:micropython:multiprocessexecution:multiprocessexecution01v01

MultiProcessExecution 01V00

Ü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

VSCode-MicroPython-Terminal:

>>>
*** 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
>>>

Globale System-Ressourcen System.py

#
# Global - Variable
#--------------------------------------------------------------
Counter = 0
#

Hauptmodul Esp32MultiProcessExecution.py

#
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')      
#

ChildProcess CountUp.py

#
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')
    #
#

ChildProcess CountDown.py

#
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')
    #
#

Library Process.py

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()
    #
#

Library Thread.py

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
    #
#   

Version

module/micropython/multiprocessexecution/multiprocessexecution01v01.txt · Last modified: 2022/09/13 11:58 by 127.0.0.1