Difficult-Rocket/tests/threading_test.py

59 lines
1.2 KiB
Python
Raw Normal View History

import os
import random
2021-04-03 12:15:24 +08:00
import time
2021-01-15 21:22:34 +08:00
2021-09-05 00:50:05 +08:00
import threading
2021-01-16 22:21:41 +08:00
2021-01-15 21:22:34 +08:00
class Aclass(threading.Thread):
def __init__(self, dev, ID):
threading.Thread.__init__(self)
self.running = True
self.value = 0
self.dev = dev
self.id = ID
print(self.id)
2021-01-16 22:21:41 +08:00
2021-01-15 21:22:34 +08:00
def run(self):
while self.running:
while self.dev.using == True:
continue
self.dev.using = True
self.running = self.dev.running[self.id]
self.dev.using = False
print(self.id, os.getpid(), os.getppid(), self.running)
time.sleep(1)
2021-01-16 22:21:41 +08:00
def stop(self):
2021-01-16 22:23:25 +08:00
self.running = False
2021-01-16 22:21:41 +08:00
2021-01-15 21:22:34 +08:00
class dev():
def __init__(self):
self.using = False
self.gets = {'a': False, 'b': False}
self.running = {'a': True, 'b': True}
2021-01-16 22:21:41 +08:00
2021-01-15 21:22:34 +08:00
Dev = dev()
A = Aclass(Dev, 'a')
time.sleep(random.random())
B = Aclass(Dev, 'b')
A.start()
B.start()
2021-02-14 20:22:29 +08:00
print('hmmm')
2021-01-16 22:21:41 +08:00
try:
while True:
In = input()
2021-02-14 20:22:29 +08:00
if In == 'stop':
2021-01-16 22:21:41 +08:00
Dev.running['a'] = False
Dev.running['b'] = False
break
try:
eval(In)
except:
print(EnvironmentError)
except KeyboardInterrupt:
A.stop()
B.stop()