From 431816ff682264650106b47d111ccb5b14859beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=91=97=E6=9D=B0?= <3695888@qq.com> Date: Sat, 16 Jan 2021 22:21:41 +0800 Subject: [PATCH] reeee --- test_threading.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) 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()