Update of /cvsroot/freevo/kaa/base/src/notifier
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28766
Modified Files:
__init__.py callback.py popen.py sockets.py thread.py timer.py
Log Message:
Ensure shutdown() is called from main thread; allow shutdown() to be called
when notifier raises exception; add "exception" and "unregistered" signals
to NotifierCallback -- attach to the exception signal of any notifier
callback (Timer, SocketDispatcher, Weak*) and it will get called with the
exception object, giving the callback an opportunity to stop the notifier
callback from being unregistered; catch exceptions in popen.killall(); add
"completed" and "exception" signals to Thread class.
Index: sockets.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/sockets.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** sockets.py 25 Jul 2005 20:02:47 -0000 1.1
--- sockets.py 2 Aug 2005 19:37:17 -0000 1.2
***************
*** 63,67 ****
return MainThreadCallback(self.unregister)()
notifier.removeSocket(self._id)
! self._id = None
--- 63,67 ----
return MainThreadCallback(self.unregister)()
notifier.removeSocket(self._id)
! super(SocketDispatcher, self).unregister()
Index: thread.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/thread.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** thread.py 2 Aug 2005 17:44:14 -0000 1.9
--- thread.py 2 Aug 2005 19:37:17 -0000 1.10
***************
*** 55,59 ****
# notifier imports
! from callback import Callback, notifier
# get logging object
--- 55,59 ----
# notifier imports
! from callback import Callback, notifier, Signal
# get logging object
***************
*** 107,112 ****
self.args = args
self.kargs = kargs
! self.result_cb = None
! self.except_cb = None
--- 107,115 ----
self.args = args
self.kargs = kargs
!
! self.signals = {
! "completed": Signal(),
! "exception": Signal()
! }
***************
*** 115,126 ****
Start the thread.
"""
! # remember callback
if callback:
! self.result_cb = MainThreadCallback(callback)
if exception_callback:
! self.except_cb = MainThreadCallback(exception_callback)
# start the thread
threading.Thread.start(self)
def run(self):
--- 118,132 ----
Start the thread.
"""
! # XXX: callback arguments should probably be deprecated
if callback:
! self.signals["completed"].connect(callback)
if exception_callback:
! self.signals["exception"].connect(callback)
# start the thread
threading.Thread.start(self)
+ def _emit_and_join(self, signal, arg):
+ self.signals[signal].emit(arg)
+ self.join()
def run(self):
***************
*** 131,144 ****
# run thread function
result = self.function(*self.args, **self.kargs)
! if self.result_cb:
! # call callback from main loop
! self.result_cb(result)
except:
log.exception('Thread raised exception:')
! if self.except_cb:
! # call callback from main loop
! self.except_cb(sys.exc_info()[1])
! # remove ourself from main
! MainThreadCallback(self.join)
--- 137,144 ----
# run thread function
result = self.function(*self.args, **self.kargs)
! MainThreadCallback(self._emit_and_join, "completed", result)()
except:
log.exception('Thread raised exception:')
! MainThreadCallback(self._emit_and_join, "exception",
sys.exc_info()[1])()
Index: timer.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/timer.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** timer.py 25 Jul 2005 20:02:47 -0000 1.1
--- timer.py 2 Aug 2005 19:37:17 -0000 1.2
***************
*** 43,52 ****
--- 43,58 ----
def start(self, interval):
+ if interval < 30:
+ # Assume interval is specified in seconds
+ interval *= 1000
+
if not is_mainthread():
return MainThreadCallback(self.start, interval)()
+
if self.active():
if not self.restart_when_active:
return
self.unregister()
+
self._id = notifier.addTimer(interval, self)
***************
*** 61,65 ****
if self.active():
notifier.removeTimer(self._id)
! self._id = None
--- 67,71 ----
if self.active():
notifier.removeTimer(self._id)
! super(Timer, self).unregister()
Index: popen.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/popen.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** popen.py 2 Aug 2005 00:35:23 -0000 1.5
--- popen.py 2 Aug 2005 19:37:17 -0000 1.6
***************
*** 431,435 ****
# now wait until all childs are dead
while self.__processes:
! notifier.step()
--- 431,440 ----
# now wait until all childs are dead
while self.__processes:
! try:
! notifier.step()
! except ( KeyboardInterrupt, SystemExit ), e:
! pass
! except:
! log.exception( 'Unhandled exception during killall' )
Index: __init__.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/__init__.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** __init__.py 2 Aug 2005 17:44:14 -0000 1.24
--- __init__.py 2 Aug 2005 19:37:17 -0000 1.25
***************
*** 92,95 ****
--- 92,99 ----
global shutting_down
+ # Ensure shutdown() is called from main thread.
+ if not is_mainthread():
+ return MainThreadCallback(shutdown)()
+
if running:
# notifier loop still running, send system exit
***************
*** 130,133 ****
--- 134,138 ----
running = True
+ e = None
while 1:
try:
***************
*** 138,146 ****
if has_signal():
log.info('Call Signal Handler')
! else:
! running = False
! raise e
running = False
shutdown()
--- 143,152 ----
if has_signal():
log.info('Call Signal Handler')
! break
!
running = False
shutdown()
+ if e:
+ raise e
Index: callback.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/callback.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** callback.py 25 Jul 2005 20:02:46 -0000 1.16
--- callback.py 2 Aug 2005 19:37:17 -0000 1.17
***************
*** 34,37 ****
--- 34,38 ----
import _weakref
import types
+ import sys
try:
***************
*** 165,168 ****
--- 166,174 ----
self._id = None
+ self.signals = {
+ "exception": Signal(),
+ "unregistered": Signal()
+ }
+
def active(self):
***************
*** 172,175 ****
--- 178,182 ----
def unregister(self):
# Unregister callback with notifier. Must be implemented by
subclasses.
+ self.signals["unregistered"].emit()
self._id = None
***************
*** 181,185 ****
return False
! ret = super(NotifierCallback, self).__call__(*args, **kwargs)
# If Notifier callbacks return False, they get unregistered.
if ret == False:
--- 188,207 ----
return False
! # If there are exception handlers for this notifier callback, we
! # catch the exception and pass it to the handler, giving it the
! # opportunity to abort the unregistering. If no handlers are
! # attached and an exception is raised, it will be propagated up to
! # our caller.
! if self.signals["exception"].count() > 0:
! try:
! ret = super(NotifierCallback, self).__call__(*args, **kwargs)
! except:
! # If any of the exception handlers return True, then the
! # object is not unregistered from the Notifier. Otherwise
! # ret = False and it will unregister.
! ret = self.signals["exception"].emit(sys.exc_info()[1])
! else:
! ret = super(NotifierCallback, self).__call__(*args, **kwargs)
!
# If Notifier callbacks return False, they get unregistered.
if ret == False:
***************
*** 250,254 ****
-
class Signal(object):
--- 272,275 ----
***************
*** 275,278 ****
--- 296,301 ----
weak = False, pos = -1):
+ assert(callable(callback))
+
if len(self._callbacks) > 40:
# It's a common problem (for me :)) that callbacks get added
***************
*** 359,362 ****
--- 382,388 ----
def emit(self, *args, **kwargs):
+ if len(self._callbacks) == 0:
+ return False
+
retval = False
for cb_callback, cb_args, cb_kwargs, cb_once, cb_weak in
self._callbacks[:]:
***************
*** 373,376 ****
--- 399,404 ----
self.disconnect(cb_callback, cb_args, cb_kwargs)
+ return retval
+
def _weakref_destroyed(self, callback, weakref):
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog