Matt Chaput avatar 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.

Comments (0)

Files changed (1)

src/whoosh/collectors.py

 """
 
 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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.