module:micropython:multiprocessexecution:multiprocessexecution01v01
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
module:micropython:multiprocessexecution:multiprocessexecution01v01 [2021/12/14 14:35] – external edit 127.0.0.1 | module: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, | ||
+ | * 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/ | ||
===== Benutzung ===== | ===== Benutzung ===== | ||
VSCode-MicroPython-Terminal: | VSCode-MicroPython-Terminal: | ||
<code python> | <code python> | ||
+ | >>> | ||
+ | *** Check Esp32MultiProcessExecution: | ||
+ | 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: | ||
+ | >>> | ||
</ | </ | ||
+ | Globale System-Ressourcen **System.py** | ||
+ | <code python> | ||
+ | # | ||
+ | # Global - Variable | ||
+ | # | ||
+ | Counter = 0 | ||
+ | # | ||
+ | </ | ||
Hauptmodul **Esp32MultiProcessExecution.py** | Hauptmodul **Esp32MultiProcessExecution.py** | ||
<code python> | <code python> | ||
+ | # | ||
+ | import time | ||
+ | # | ||
+ | import Process as PRC | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | if (' | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | ProcessCU = PRC.CProcess(' | ||
+ | ProcessCD = PRC.CProcess(' | ||
+ | ProcessCU.Start() | ||
+ | time.sleep(0.1) | ||
+ | ProcessCD.Start() | ||
+ | time.sleep(3.0) | ||
+ | ProcessCU.Abort() | ||
+ | ProcessCD.Abort() | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
</ | </ | ||
- | ChildProcess **BlinkFast.py** | + | ChildProcess **CountUp.py** |
<code python> | <code python> | ||
+ | # | ||
+ | import time | ||
+ | # | ||
+ | import System | ||
+ | # | ||
+ | # | ||
+ | # Main | ||
+ | # | ||
+ | if (' | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | for I in range(0, 10): | ||
+ | System.Counter += 1 | ||
+ | print(' | ||
+ | time.sleep(0.1) | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | # | ||
</ | </ | ||
- | ChildProcess **BlinkSlow.py** | + | ChildProcess **CountDown.py** |
<code python> | <code python> | ||
+ | # | ||
+ | import time | ||
+ | # | ||
+ | import System | ||
+ | # | ||
+ | # | ||
+ | # Main | ||
+ | # | ||
+ | if (' | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | for I in range(0, 10): | ||
+ | System.Counter -= 1 | ||
+ | print(' | ||
+ | time.sleep(0.15) | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | # | ||
</ | </ | ||
- | ChildProcess | + | Library |
- | <code python> | + | <code python> |
- | </ | + | import time |
- | + | # | |
- | ChildProcess **CountDown.py** | + | import Thread as THR |
- | <code python> | + | # |
+ | class CProcess(): | ||
+ | # | ||
+ | def __init__(self, | ||
+ | self.ID = processid | ||
+ | | ||
+ | self.Thread = THR.CThread(self.CBOnStart, | ||
+ | self.CBOnAbort, | ||
+ | # | ||
+ | def CBOnStart(self, | ||
+ | print(' | ||
+ | return | ||
+ | def CBOnBusy(self, | ||
+ | print(' | ||
+ | execfile(self.Filename) | ||
+ | return | ||
+ | def CBOnAbort(self, | ||
+ | print(' | ||
+ | return | ||
+ | def CBOnEnd(self, | ||
+ | print(' | ||
+ | return | ||
+ | # | ||
+ | def GetID(self): | ||
+ | return self.ID | ||
+ | # | ||
+ | def Start(self): | ||
+ | self.Thread.Start() | ||
+ | # | ||
+ | def Abort(self): | ||
+ | self.Thread.Abort() | ||
+ | # | ||
+ | # | ||
</ | </ | ||
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, | ||
+ | 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, | ||
+ | return | ||
+ | # | ||
+ | def Abort(self): | ||
+ | self.State = stEnd | ||
+ | if (None != self.OnAbort): | ||
+ | self.OnAbort(self) | ||
+ | return | ||
+ | # | ||
+ | def CBOnExecute(self, | ||
+ | if (None != self.OnBusy): | ||
+ | self.OnBusy(self) | ||
+ | if (None != self.OnEnd): | ||
+ | self.OnEnd(self) | ||
+ | return | ||
+ | # | ||
+ | # | ||
</ | </ | ||
Line 45: | Line 226: | ||
===== Version ===== | ===== Version ===== | ||
- | {{: | + | {{: |
----- | ----- |
module/micropython/multiprocessexecution/multiprocessexecution01v01.1639488910.txt.gz · Last modified: 2021/12/14 15:35 (external edit)