Python3 杀死一个被阻塞的线程



所以我正在做一个估计总共只能执行 10 秒的任务。

我列出了一个threading.Thread,所有访问queue.Queue来拉取任务,但在 10 秒结束后,我想杀死线程并继续前进,但是,如果我先完成,我想在 10 秒之前继续前进。

我在呼叫中阻止了一个"管理器"线程queue.Queue这样我就可以在 10 秒结束之前查看队列是否完成。

self._task_queue.join()

如果已达到超时,如何让主执行线程停止该线程?注意 我使用threading.Thread因为这些线程正在执行的任务是检查操作系统任务。

例:

timeout_dt = datetime.utcnow() + timedelta(seconds=self._timeout) if self._timeout else None
while True:
try:
current_time = datetime.utcnow()
if self._manager.event.is_set():
logger.info(f'manager complete event {self._manager.event.is_set()}')
break
if timeout_dt and current_time > timeout_dt:
logger.info(f'timeout complete current {current_time} timeout {timeout_dt}')
# TODO: kill the manager thread waiting for queue.Queue.join()
break
time.sleep(1)
except Exception as e:
# An exception happened in the main execution thread
logger.exception(e)

我是如何解决的

class MyQueue(Queue):
def timeout_join(self, timeout=None):
'''Blocks until all items in the Queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer thread calls task_done()
to indicate the item was retrieved and all work on it is complete. If
optional args 'timeout' is a non-negative number, it blocks at most
'timeout' seconds and raises the TimeoutError exception if the number
of unfinished tasks is not equal to the task_done in the available time.
When the count of unfinished tasks drops to zero or timeout is reached,
join() unblocks.
'''
with self.all_tasks_done:
if timeout is None:
while self.unfinished_tasks:
self.all_tasks_done.wait()
elif timeout and timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
endtime = time.time() + timeout
while self.unfinished_tasks:
remaining = endtime - time.time()
if remaining <= 0.0:
raise TimeoutError
self.all_tasks_done.wait(remaining)

最新更新