Created
April 11, 2023 15:17
-
-
Save Friedjof/878fe6cb6cd0eb790b9b55e19fc32adf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import * | |
from time import sleep | |
class States: | |
loop: bool = False | |
class Loop(Thread): | |
def __init__(self, states: States, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.states = states | |
def run(self) -> None: | |
counter: int = 0 | |
# Die Schleife so lange ausführen wie 'States.loop' True ist | |
while self.states.loop: | |
counter += 1 | |
print(f"{self.name}: loop - {counter}") | |
sleep(1) | |
class Controller: | |
def __init__(self): | |
self.states: States = States() | |
self.loop: Loop = Loop(self.states, name="LOOP THREAD") | |
# Starten des Threads | |
def start(self) -> None: | |
self.states.loop = True | |
self.loop.start() | |
# Setzt den 'loop-status' auf False | |
def stop(self): | |
self.states.loop = False | |
if __name__ == '__main__': | |
# Controller initialisieren | |
c: Controller = Controller() | |
# Starte den Loop-Thread | |
c.start() | |
# Warte 3 Sekunden | |
sleep(3) | |
# Stoppe den Thread | |
c.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment