diff --git a/test_threading.py b/test_threading.py index dbd69dc..0e9cc1f 100644 --- a/test_threading.py +++ b/test_threading.py @@ -2,8 +2,10 @@ import threading import time import os +import sys import random + class Aclass(threading.Thread): def __init__(self, dev, ID): threading.Thread.__init__(self) @@ -12,7 +14,7 @@ class Aclass(threading.Thread): self.dev = dev self.id = ID print(self.id) - + def run(self): while self.running: while self.dev.using == True: @@ -23,12 +25,17 @@ class Aclass(threading.Thread): print(self.id, os.getpid(), os.getppid(), self.running) time.sleep(1) + def stop(self): + sys.exit() + + class dev(): def __init__(self): self.using = False self.gets = {'a': False, 'b': False} self.running = {'a': True, 'b': True} + Dev = dev() A = Aclass(Dev, 'a') time.sleep(random.random()) @@ -36,13 +43,17 @@ B = Aclass(Dev, 'b') A.start() B.start() print("hmmm") -while True: - In = input() - if In == "stop": - Dev.running['a'] = False - Dev.running['b'] = False - break - try: - eval(In) - except: - print(EnvironmentError) \ No newline at end of file +try: + while True: + In = input() + if In == "stop": + Dev.running['a'] = False + Dev.running['b'] = False + break + try: + eval(In) + except: + print(EnvironmentError) +except KeyboardInterrupt: + A.stop() + B.stop()