mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +00:00
Remove ThreadPoolExecutor shutdown
backport (#66735)
This commit is contained in:
parent
f8d38a1025
commit
cdd5d22b38
@ -4,11 +4,11 @@ from __future__ import annotations
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
import contextlib
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
import queue
|
|
||||||
import sys
|
import sys
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from .thread import async_raise
|
from .thread import async_raise
|
||||||
|
|
||||||
@ -62,29 +62,9 @@ def join_or_interrupt_threads(
|
|||||||
class InterruptibleThreadPoolExecutor(ThreadPoolExecutor):
|
class InterruptibleThreadPoolExecutor(ThreadPoolExecutor):
|
||||||
"""A ThreadPoolExecutor instance that will not deadlock on shutdown."""
|
"""A ThreadPoolExecutor instance that will not deadlock on shutdown."""
|
||||||
|
|
||||||
def shutdown(self, *args, **kwargs) -> None: # type: ignore
|
def shutdown(self, *args: Any, **kwargs: Any) -> None:
|
||||||
"""Shutdown backport from cpython 3.9 with interrupt support added."""
|
"""Shutdown with interrupt support added."""
|
||||||
with self._shutdown_lock:
|
super().shutdown(wait=False, cancel_futures=True)
|
||||||
self._shutdown = True
|
|
||||||
# Drain all work items from the queue, and then cancel their
|
|
||||||
# associated futures.
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
work_item = self._work_queue.get_nowait()
|
|
||||||
except queue.Empty:
|
|
||||||
break
|
|
||||||
if work_item is not None:
|
|
||||||
work_item.future.cancel()
|
|
||||||
# Send a wake-up to prevent threads calling
|
|
||||||
# _work_queue.get(block=True) from permanently blocking.
|
|
||||||
self._work_queue.put(None) # type: ignore[arg-type]
|
|
||||||
|
|
||||||
# The above code is backported from python 3.9
|
|
||||||
#
|
|
||||||
# For maintainability join_threads_or_timeout is
|
|
||||||
# a separate function since it is not a backport from
|
|
||||||
# cpython itself
|
|
||||||
#
|
|
||||||
self.join_threads_or_timeout()
|
self.join_threads_or_timeout()
|
||||||
|
|
||||||
def join_threads_or_timeout(self) -> None:
|
def join_threads_or_timeout(self) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user