class CountDownLatch(object): def __init__(self): self.con = Condition() # 条件变量 def wait(self): with self.con: self.con.wait() def countDown(self): with self.con: self.con.notify_all() # 开枪(唤醒所有线程)
def run( input_image: Array, new_input_image: Condition, input_size: (int, int), output_image: Array, new_output_image: Condition, output_size: (int, int), ): while True: with new_input_image: new_input_image.wait() with input_image.get_lock(): image = Image.frombuffer("RGB", input_size, input_image.get_obj()) result = image.resize(output_size, resample=Image.NEAREST) with output_image.get_lock(): dest = numpy.frombuffer(output_image.get_obj(), dtype=numpy.uint8).reshape( (output_size[0], output_size[1], 3) ) numpy.copyto(dest, numpy.array(result)) with new_output_image: new_output_image.notify_all()