Python3杀死阻塞的线程

Python3杀死阻塞的线程,python,python-3.x,multithreading,Python,Python 3.x,Multithreading,因此,我正在进行一项任务,估计总共只能执行10秒 我列出了threading.Thread,所有这些线程都访问queue.queue来提取任务,但在10秒之后,我想杀死线程并继续前进,但如果我先完成,我想在10秒之前继续前进 我在队列上有一个“管理器”线程被阻塞。通过这种方式,我可以在10秒结束之前查看队列是否已完成 self._task_queue.join() 如果已达到超时,如何让主执行线程停止该线程?注意我使用的是线程。线程,因为这些线程执行的任务是检查操作系统任务 例如: timeo

因此,我正在进行一项任务,估计总共只能执行10秒

我列出了
threading.Thread
,所有这些线程都访问
queue.queue
来提取任务,但在10秒之后,我想杀死线程并继续前进,但如果我先完成,我想在10秒之前继续前进

我在
队列上有一个“管理器”线程被阻塞。通过这种方式,我可以在10秒结束之前查看队列是否已完成

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

例如:

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)
类MyQueue(队列):
def timeout_join(自身,超时=无):
''阻塞,直到队列中的所有项目都已获取并处理完毕。
每当将项目添加到列表中时,未完成任务的计数就会增加
队列每当使用者线程调用task_done()时,计数就会下降
以指示已检索该项,并且该项上的所有工作都已完成。如果
可选参数“timeout”是一个非负数,它最多会阻塞
“timeout”秒,如果数字为
未完成任务的数量不等于可用时间内完成的任务数量。
当未完成任务的计数降至零或达到超时时,
join()取消阻塞。
'''
完成self.all_任务后:
如果超时为无:
而self.unfinished_任务:
self.all_tasks_done.wait()
elif超时和超时<0:
raise VALUERROR(“'timeout'必须是非负数”)
其他:
endtime=time.time()+超时
而self.unfinished_任务:
剩余=endtime-time.time()

如果剩余的话,我认为使用queue.queue不可能做到这一点。看一看它的shutdown(cancel_futures=True)方法。这和你要找的很接近吗?