Matt Chaput committed f47fcc3

Changed TimeLimitCollector to require a keyword arg to use signals.
Don't import signal except in TimeLimitCollector when use_alarm=True.

  • Participants
  • Parent commits c2f1f05

Comments (0)

Files changed (1)


 import os
-import signal
 import threading
 from array import array
 from bisect import insort
     is slow the search could exceed the time limit.
-    def __init__(self, child, timelimit, greedy=False):
+    def __init__(self, child, timelimit, greedy=False, use_alarm=True):
         :param child: the collector to wrap.
         :param timelimit: the maximum amount of time (in seconds) to
             raise a ``TimeLimit`` exception.
         :param greedy: if ``True``, the collector will finish adding the most
             recent hit before raising the ``TimeLimit`` exception.
+        :param use_alarm: if ``True`` (the default), the collector will try to
+            use signal.SIGALRM (on UNIX).
         self.child = child
         self.timelimit = timelimit
         self.greedy = greedy
-        self.use_alarm = hasattr(signal, "SIGALRM")
+        if use_alarm:
+            import signal
+            self.use_alarm = use_alarm and hasattr(signal, "SIGALRM")
+        self.timer = None
+        self.timedout = False
     def prepare(self, top_searcher, q, context):
         self.child.prepare(top_searcher, q, context)
         self.timedout = False
         if self.use_alarm:
+            import signal
             signal.signal(signal.SIGALRM, self._was_signaled)
         # Start a timer thread. If the timer fires, it will call this object's
         self.timedout = True
         if self.use_alarm:
+            import signal
             os.kill(os.getpid(), signal.SIGALRM)
     def _was_signaled(self, signum, frame):