From a8d71fe3b1694c55a165d4ffc52df0a5789706e3 Mon Sep 17 00:00:00 2001 From: Ginkss Date: Tue, 15 Oct 2024 15:09:19 +1100 Subject: [PATCH] Update threading.py Timer Add support for context manager. Start on entry, cancel on exit --- Lib/threading.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/threading.py b/Lib/threading.py index 94ea2f08178369..744f34653f5476 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1342,6 +1342,12 @@ def run(self): self.function(*self.args, **self.kwargs) self.finished.set() + def __enter__(self): + self.start() + + def __exit__(self, exc_type, exc_val, exc_tb): + self.cancel() + # Special thread class to represent the main thread