解决方案很简单,我想了一整夜,但当我开始冷静地解决这个问题时,我做到了!
def queue(self, student, first_teacher, second_teacher, lock1, lock2):
student.take_test()
while True:
if not lock1.locked():
with lock1:
first_teacher.check_test(student)
if student.get_result():
print(f"student {student.get_id()} passed the test - work checked by {first_teacher.name}")
# self.tested.remove(student)
return
threading.Thread(target=self.queue, args=(
student, self.first_teacher, self.second_teacher, lock1, lock2))
if not lock2.locked():
with lock2:
second_teacher.check_test(student)
if student.get_result():
print(f"student {student.get_id()} passed the test - work checked by {second_teacher.name}")
# self.tested.remove(student)
return
threading.Thread(target=self.queue, args=(
student, self.first_teacher, self.second_teacher, lock1, lock2))
def run_test(self):
lock1 = threading.Lock()
lock2 = threading.Lock()
threads = []
for student in self.tested:
t = threading.Thread(target=self.queue, args=(
student, self.first_teacher, self.second_teacher, lock1, lock2))
threads.append(t)
t.start()
for thread in threads:
thread.join()