module:micropython:multiprocessexecution:multiprocessexecution01v01
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| module:micropython:multiprocessexecution:multiprocessexecution01v01 [2021/12/14 16:12] – [Benutzung] omdevelop | 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> | ||
| - | </ | ||
| - | |||
| - | |||
| - | Hauptmodul **Esp32MultiProcessExecution.py** | ||
| <code python> | <code python> | ||
| >>> | >>> | ||
| Line 57: | Line 55: | ||
| </ | </ | ||
| - | ChildProcess | + | Globale System-Ressourcen |
| - | <code python> | + | <code python> |
| + | # | ||
| + | # Global - Variable | ||
| + | # | ||
| + | Counter = 0 | ||
| + | # | ||
| </ | </ | ||
| - | ChildProcess | + | Hauptmodul |
| - | <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 **CountUp.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 **CountDown.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(' | ||
| + | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | Library **Process.py** | ||
| + | <code python> | ||
| + | import time | ||
| + | # | ||
| + | import Thread as THR | ||
| + | # | ||
| + | class CProcess(): | ||
| + | # | ||
| + | def __init__(self, | ||
| + | self.ID = processid | ||
| + | self.Filename = filename | ||
| + | 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 80: | Line 226: | ||
| ===== Version ===== | ===== Version ===== | ||
| - | {{: | + | {{: |
| ----- | ----- | ||
module/micropython/multiprocessexecution/multiprocessexecution01v01.1639494744.txt.gz · Last modified: 2021/12/14 17:12 (external edit)