MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / _Timeout

Class _Timeout

bench.py:43–82  ·  view source on GitHub ↗

Context-manager wall-clock timeout. Works on both Unix (SIGALRM) and Windows (thread-based fallback).

Source from the content-addressed store, hash-verified

41
42
43class _Timeout:
44 """Context-manager wall-clock timeout. Works on both Unix (SIGALRM) and
45 Windows (thread-based fallback)."""
46
47 def __init__(self, seconds: int):
48 self.seconds = seconds
49 self._use_signal = hasattr(signal, "SIGALRM")
50
51 # --- signal-based (Unix) -------------------------------------------
52 def _handler(self, signum, frame):
53 raise BenchTimeoutError(f"Timed out after {self.seconds}s")
54
55 def __enter__(self):
56 if self._use_signal:
57 self._old = signal.signal(signal.SIGALRM, self._handler)
58 signal.alarm(self.seconds)
59 else:
60 import threading
61 self._timer = threading.Timer(self.seconds, self._timeout_thread)
62 self._timer.daemon = True
63 self._timed_out = False
64 self._timer.start()
65 return self
66
67 def __exit__(self, *exc):
68 if self._use_signal:
69 signal.alarm(0)
70 signal.signal(signal.SIGALRM, self._old)
71 else:
72 self._timer.cancel()
73 return False
74
75 def _timeout_thread(self):
76 self._timed_out = True
77 # On Windows we cannot forcefully interrupt the main thread the same
78 # way SIGALRM does. We set a flag; callers that iterate can check it.
79 # For truly blocking GPU calls, this will not help -- but at least
80 # the outer try/except will catch it after the call returns.
81 import _thread
82 _thread.interrupt_main()
83
84
85# =========================================================================

Callers 2

run_correctnessFunction · 0.70
run_performanceFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected