Add timeout argument to python's Queue.join()

前端 未结 3 2046
攒了一身酷
攒了一身酷 2020-12-28 17:43

I want to be able to join() the Queue class but timeouting after some time if the call hasn\'t returned yet. What is the best way to do it? Is it possible to do it by subcla

3条回答
  •  醉酒成梦
    2020-12-28 18:27

    Subclassing Queue is probably the best way. Something like this should work (untested):

    def join_with_timeout(self, timeout):
        self.all_tasks_done.acquire()
        try:
            endtime = time() + timeout
            while self.unfinished_tasks:
                remaining = endtime - time()
                if remaining <= 0.0:
                    raise NotFinished
                self.all_tasks_done.wait(remaining)
        finally:
            self.all_tasks_done.release()
    

提交回复
热议问题