Author: dmeyer
Date: Tue Feb 19 03:01:04 2008
New Revision: 3118

Log:
move timeout parameter to loop()

Modified:
   trunk/base/src/notifier/async.py
   trunk/base/src/notifier/main.py

Modified: trunk/base/src/notifier/async.py
==============================================================================
--- trunk/base/src/notifier/async.py    (original)
+++ trunk/base/src/notifier/async.py    Tue Feb 19 03:01:04 2008
@@ -368,18 +368,11 @@
         """
         # Import modules here rather than globally to avoid circular importing.
         import main
-        from thread import set_as_mainthread, is_mainthread
+        from thread import is_mainthread
         if is_mainthread() or not main.is_running():
             # We're waiting in the main thread, so we must keep the mainloop
-            # alive by calling step() until we're finished.
-            abort = []
-            if timeout:
-                # Add a timer to make sure the notifier doesn't sleep
-                # beyond out timeout.
-                from timer import OneShotTimer
-                OneShotTimer(lambda: abort.append(True)).start(timeout)
-
-            main.loop(lambda: not self.is_finished() and not abort)
+            # alive by calling main.loop() until we're finished.
+            main.loop(lambda: not self.is_finished(), timeout)
         else:
             # We're waiting in some other thread, so wait for some other
             # thread to wake us up.

Modified: trunk/base/src/notifier/main.py
==============================================================================
--- trunk/base/src/notifier/main.py     (original)
+++ trunk/base/src/notifier/main.py     Tue Feb 19 03:01:04 2008
@@ -41,6 +41,7 @@
 
 import nf_wrapper as notifier
 from signals import Signal
+from timer import OneShotTimer
 from popen import proclist as _proclist
 from thread import is_mainthread, wakeup, set_as_mainthread, threaded, 
MAINTHREAD
 from thread import killall as kill_jobserver
@@ -80,21 +81,32 @@
     return notifier.init( module, **options )
 
 
-def loop(condition):
+def loop(condition, timeout = None):
     """
     Executes the main loop until condition is met.  condition is either a
     callable, or value that is evaluated after each step of the main loop.
     """
     unhandled_exception = None
+    initial_mainloop = False
 
     if not is_running():
+        # no mainloop is running, set this thread as mainloop and
+        # set the internal running state.
+        initial_mainloop = True
         set_as_mainthread()
         _set_running(True)
 
     if not callable(condition):
         condition = lambda: condition
 
-    while condition():
+    abort = []
+    if timeout is not None:
+        # timeout handling to stop the mainloop after the given timeout
+        # even when the condition is still True.
+        timeout = OneShotTimer(lambda: abort.append(True))
+        timeout.start(timeout)
+
+    while condition() and not abort:
         try:
             notifier.step()
         except (KeyboardInterrupt, SystemExit):
@@ -117,7 +129,12 @@
                 unhandled_exception = sys.exc_info()
                 break
 
-    _set_running(False)
+    if timeout is not None:
+        timeout.stop(timeout)
+
+    if initial_mainloop:
+        _set_running(False)
+
     if unhandled_exception:
         # We aborted the main loop due to an unhandled exception.  Now
         # that we've cleaned up, we can reraise the exception.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to