mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Clean up no longer needed Python 3.8 support code (#65231)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
9825111c8d
commit
872bc456a9
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
import asyncio
|
import asyncio
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from functools import lru_cache
|
from functools import cache
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
@ -561,34 +561,32 @@ def _update( # noqa: C901
|
|||||||
return state, value, update_time
|
return state, value, update_time
|
||||||
|
|
||||||
|
|
||||||
# When we drop python 3.8 support these can be switched to
|
@cache
|
||||||
# @cache https://docs.python.org/3.9/library/functools.html#functools.cache
|
|
||||||
@lru_cache(maxsize=None)
|
|
||||||
def _disk_usage(path: str) -> Any:
|
def _disk_usage(path: str) -> Any:
|
||||||
return psutil.disk_usage(path)
|
return psutil.disk_usage(path)
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@cache
|
||||||
def _swap_memory() -> Any:
|
def _swap_memory() -> Any:
|
||||||
return psutil.swap_memory()
|
return psutil.swap_memory()
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@cache
|
||||||
def _virtual_memory() -> Any:
|
def _virtual_memory() -> Any:
|
||||||
return psutil.virtual_memory()
|
return psutil.virtual_memory()
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@cache
|
||||||
def _net_io_counters() -> Any:
|
def _net_io_counters() -> Any:
|
||||||
return psutil.net_io_counters(pernic=True)
|
return psutil.net_io_counters(pernic=True)
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@cache
|
||||||
def _net_if_addrs() -> Any:
|
def _net_if_addrs() -> Any:
|
||||||
return psutil.net_if_addrs()
|
return psutil.net_if_addrs()
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@cache
|
||||||
def _getloadavg() -> tuple[float, float, float]:
|
def _getloadavg() -> tuple[float, float, float]:
|
||||||
return os.getloadavg()
|
return os.getloadavg()
|
||||||
|
|
||||||
|
@ -15,8 +15,8 @@ from .util.executor import InterruptibleThreadPoolExecutor
|
|||||||
from .util.thread import deadlock_safe_shutdown
|
from .util.thread import deadlock_safe_shutdown
|
||||||
|
|
||||||
#
|
#
|
||||||
# Python 3.8 has significantly less workers by default
|
# Some Python versions may have different number of workers by default
|
||||||
# than Python 3.7. In order to be consistent between
|
# than others. In order to be consistent between
|
||||||
# supported versions, we need to set max_workers.
|
# supported versions, we need to set max_workers.
|
||||||
#
|
#
|
||||||
# In most cases the workers are not I/O bound, as they
|
# In most cases the workers are not I/O bound, as they
|
||||||
@ -121,9 +121,7 @@ def run(runtime_config: RuntimeConfig) -> int:
|
|||||||
try:
|
try:
|
||||||
_cancel_all_tasks_with_timeout(loop, TASK_CANCELATION_TIMEOUT)
|
_cancel_all_tasks_with_timeout(loop, TASK_CANCELATION_TIMEOUT)
|
||||||
loop.run_until_complete(loop.shutdown_asyncgens())
|
loop.run_until_complete(loop.shutdown_asyncgens())
|
||||||
# Once cpython 3.8 is no longer supported we can use the
|
loop.run_until_complete(loop.shutdown_default_executor())
|
||||||
# the built-in loop.shutdown_default_executor
|
|
||||||
loop.run_until_complete(_shutdown_default_executor(loop))
|
|
||||||
finally:
|
finally:
|
||||||
asyncio.set_event_loop(None)
|
asyncio.set_event_loop(None)
|
||||||
loop.close()
|
loop.close()
|
||||||
@ -159,22 +157,3 @@ def _cancel_all_tasks_with_timeout(
|
|||||||
"task": task,
|
"task": task,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _shutdown_default_executor(loop: asyncio.AbstractEventLoop) -> None:
|
|
||||||
"""Backport of cpython 3.9 schedule the shutdown of the default executor."""
|
|
||||||
future = loop.create_future()
|
|
||||||
|
|
||||||
def _do_shutdown() -> None:
|
|
||||||
try:
|
|
||||||
loop._default_executor.shutdown(wait=True) # type: ignore # pylint: disable=protected-access
|
|
||||||
loop.call_soon_threadsafe(future.set_result, None)
|
|
||||||
except Exception as ex: # pylint: disable=broad-except
|
|
||||||
loop.call_soon_threadsafe(future.set_exception, ex)
|
|
||||||
|
|
||||||
thread = threading.Thread(target=_do_shutdown)
|
|
||||||
thread.start()
|
|
||||||
try:
|
|
||||||
await future
|
|
||||||
finally:
|
|
||||||
thread.join()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user