uranusjr commented on a change in pull request #16110:
URL: https://github.com/apache/airflow/pull/16110#discussion_r649849547



##########
File path: airflow/utils/timeout.py
##########
@@ -22,12 +22,43 @@
 from airflow.utils.platform import IS_WINDOWS
 from airflow.exceptions import AirflowTaskTimeout
 from airflow.utils.log.logging_mixin import LoggingMixin
+from typing import ContextManager, Type
 
-TIMER_THREAD_ATTR = '_timer_thread'
+_timeout = ContextManager[None]
 
 
-class timeout(LoggingMixin):  # pylint: disable=invalid-name
-    """To be used in a ``with`` block and timeout its content."""
+class _timeout_windows(_timeout, LoggingMixin):
+
+    def __init__(self, seconds=1, error_message='Timeout'):
+        super().__init__()
+        self._timer: Timer = None
+        self.seconds = seconds
+        self.error_message = error_message + ': Operation timed out.'
+
+    def handle_timeout(self, *args):  # pylint: disable=unused-argument
+        """Logs information and raises AirflowTaskTimeout."""
+        self.log.error("Operation timed out.")
+        raise AirflowTaskTimeout(self.error_message)
+
+    def __enter__(self):
+        try:
+            if self._timer:
+                self._timer.cancel()
+            self._timer = Timer(self.seconds, self.handle_timeout)
+            self._timer.start()
+        except ValueError:

Review comment:
       Does this ever happen?

##########
File path: airflow/utils/timeout.py
##########
@@ -22,12 +22,43 @@
 from airflow.utils.platform import IS_WINDOWS
 from airflow.exceptions import AirflowTaskTimeout
 from airflow.utils.log.logging_mixin import LoggingMixin
+from typing import ContextManager, Type
 
-TIMER_THREAD_ATTR = '_timer_thread'
+_timeout = ContextManager[None]
 
 
-class timeout(LoggingMixin):  # pylint: disable=invalid-name
-    """To be used in a ``with`` block and timeout its content."""
+class _timeout_windows(_timeout, LoggingMixin):
+
+    def __init__(self, seconds=1, error_message='Timeout'):
+        super().__init__()
+        self._timer: Timer = None
+        self.seconds = seconds
+        self.error_message = error_message + ': Operation timed out.'

Review comment:
       Why does this not show the PID like the POSIX version?

##########
File path: airflow/utils/timeout.py
##########
@@ -22,12 +22,43 @@
 from airflow.utils.platform import IS_WINDOWS
 from airflow.exceptions import AirflowTaskTimeout
 from airflow.utils.log.logging_mixin import LoggingMixin
+from typing import ContextManager, Type
 
-TIMER_THREAD_ATTR = '_timer_thread'
+_timeout = ContextManager[None]
 
 
-class timeout(LoggingMixin):  # pylint: disable=invalid-name
-    """To be used in a ``with`` block and timeout its content."""
+class _timeout_windows(_timeout, LoggingMixin):
+
+    def __init__(self, seconds=1, error_message='Timeout'):
+        super().__init__()
+        self._timer: Timer = None

Review comment:
       I don’t think mypy would allow this




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to