From 61fa984166fbc87f70ef909f45eee34ad7c371bf Mon Sep 17 00:00:00 2001 From: David Viana Date: Sat, 13 May 2023 15:06:33 +0100 Subject: [PATCH 1/2] Avoid importing resource on win32 --- fastapi_utils/timing.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fastapi_utils/timing.py b/fastapi_utils/timing.py index f015d8ac..a6b9f7ab 100644 --- a/fastapi_utils/timing.py +++ b/fastapi_utils/timing.py @@ -9,7 +9,11 @@ """ from __future__ import annotations -import resource +import sys + +if sys.platform != "win32": + import resource + import time from collections.abc import Callable from typing import Any From 0d1418379ab66fd436882fe6f1f15e20e1dee78c Mon Sep 17 00:00:00 2001 From: David Viana Date: Sat, 13 May 2023 15:07:18 +0100 Subject: [PATCH 2/2] Add _get_cpu_time win32 specific implementation --- fastapi_utils/timing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fastapi_utils/timing.py b/fastapi_utils/timing.py index a6b9f7ab..41e939b6 100644 --- a/fastapi_utils/timing.py +++ b/fastapi_utils/timing.py @@ -186,6 +186,9 @@ def _get_cpu_time() -> float: """ Generates the cpu time to report. Adds the user and system time, following the implementation from timing-asgi """ + if sys.platform == "win32": + return time.process_time() + resources = resource.getrusage(resource.RUSAGE_SELF) # add up user time (ru_utime) and system time (ru_stime) return resources[0] + resources[1]