Author: tack
Date: Mon Feb 18 21:13:19 2008
New Revision: 3115
Log:
New function main.loop(), which is mostly what main.run() used to be.
Update InProgress.wait() to use this function.
This fixes a nasty bug where two threads call wait() on an InProgress
object before main.run() is started. Each one thinks it should set
itself to be the main thread.
main.step() should not usually be called. All instances of this idiom:
while some_condition:
main.step()
should be replaced with this one:
main.loop(lambda: some_condition)
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 Mon Feb 18 21:13:19 2008
@@ -369,14 +369,7 @@
# Import modules here rather than globally to avoid circular importing.
import main
from thread import set_as_mainthread, is_mainthread
-
- if not main.is_running():
- # No main loop is running yet. We're calling step() below,
- # but we won't get notified of any thread completion
- # unless the thread notifier pipe is initialized.
- set_as_mainthread()
-
- if 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 = []
@@ -386,8 +379,8 @@
from timer import OneShotTimer
OneShotTimer(lambda: abort.append(True)).start(timeout)
- while not self.is_finished() and not abort:
- main.step()
+ import threading
+ main.loop(lambda: not self.is_finished() and not abort)
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 Mon Feb 18 21:13:19 2008
@@ -46,7 +46,7 @@
from thread import killall as kill_jobserver
__all__ = [ 'run', 'stop', 'step', 'select_notifier', 'is_running', 'wakeup',
- 'set_as_mainthread', 'is_shutting_down' ]
+ 'set_as_mainthread', 'is_shutting_down', 'loop' ]
# get logging object
log = logging.getLogger('notifier')
@@ -57,7 +57,6 @@
# Set if currently in shutdown() (to prevent reentrancy)
_shutting_down = False
-
def _step_signal_changed(signal, flag):
if flag == Signal.SIGNAL_CONNECTED and signal.count() == 1:
notifier.dispatcher_add(signals["step"].emit)
@@ -81,20 +80,21 @@
return notifier.init( module, **options )
-def run():
+def loop(condition):
"""
- Notifier main loop function. It will loop until an exception
- is raised or sys.exit is called.
+ 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
- if is_running():
- raise RuntimeError('Mainthread is already running')
+ if not is_running():
+ set_as_mainthread()
+ _set_running(True)
- _set_running(True)
- set_as_mainthread()
+ if not callable(condition):
+ condition = lambda: condition
- while True:
+ while condition():
try:
notifier.step()
except (KeyboardInterrupt, SystemExit):
@@ -118,7 +118,6 @@
break
_set_running(False)
- stop()
if unhandled_exception:
# We aborted the main loop due to an unhandled exception. Now
# that we've cleaned up, we can reraise the exception.
@@ -126,6 +125,20 @@
raise type, value, tb
+def run():
+ """
+ Notifier main loop function. It will loop until an exception
+ is raised or sys.exit is called.
+ """
+ if is_running():
+ raise RuntimeError('Mainthread is already running')
+
+ try:
+ loop(True)
+ finally:
+ stop()
+
+
# Ensure stop() is called from main thread.
@threaded(MAINTHREAD)
def stop():
-------------------------------------------------------------------------
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