Author: dmeyer
Date: Mon Oct 29 14:28:26 2007
New Revision: 2893
Log:
fix shutdown and add special twisted support to threading
Modified:
trunk/base/src/notifier/__init__.py
trunk/base/src/notifier/nf_thread.py
trunk/base/test/kaa_in_twisted.py
Modified: trunk/base/src/notifier/__init__.py
==============================================================================
--- trunk/base/src/notifier/__init__.py (original)
+++ trunk/base/src/notifier/__init__.py Mon Oct 29 14:28:26 2007
@@ -143,9 +143,9 @@
"""
Init the notifier.
"""
- if module == 'thread':
+ if module in ('thread', 'twisted'):
import nf_thread
- return nf_thread.init(options['handler'])
+ return nf_thread.init(module, **options)
return notifier.init( module, **options )
Modified: trunk/base/src/notifier/nf_thread.py
==============================================================================
--- trunk/base/src/notifier/nf_thread.py (original)
+++ trunk/base/src/notifier/nf_thread.py Mon Oct 29 14:28:26 2007
@@ -9,15 +9,23 @@
class ThreadLoop(threading.Thread):
- def __init__(self, interleave):
+ def __init__(self, interleave, shutdown = None):
super(ThreadLoop, self).__init__()
self.interleave = interleave
self.condition = threading.Semaphore(0)
self.sleeping = False
+ self.shutdown = kaa.notifier.shutdown
+ if shutdown:
+ self.shutdown = shutdown
def handle(self):
- nf_wrapper.step(sleep = False)
- self.condition.release()
+ try:
+ try:
+ nf_wrapper.step(sleep = False)
+ except (KeyboardInterrupt, SystemExit):
+ kaa.notifier.running = False
+ finally:
+ self.condition.release()
def run(self):
kaa.notifier.running = True
@@ -30,12 +38,14 @@
break
self.interleave(self.handle)
self.condition.acquire()
+ if not kaa.notifier.running:
+ break
except (KeyboardInterrupt, SystemExit):
pass
except Exception, e:
log.exception('loop')
kaa.notifier.running = False
- kaa.notifier.shutdown()
+ self.interleave(self.shutdown)
class Wakeup(object):
@@ -50,12 +60,25 @@
return ret
-def init( handler ):
+def get_handler(module):
+ """
+ Use the thread based mainloop with twisted.
+ """
+ if module == 'twisted':
+ # get reactor and return callback
+ from twisted.internet import reactor
+ return reactor.callFromThread, reactor.stop
+ raise RuntimeError('no handler defined for thread mainloop')
+
+
+def init( module, handler = None, shutdown = None, **options ):
"""
Init the notifier.
"""
- loop = ThreadLoop(handler)
- nf_wrapper.init( 'generic', use_pynotifier=False )
+ if handler == None:
+ handler, shutdown = get_handler(module)
+ loop = ThreadLoop(handler, shutdown)
+ nf_wrapper.init( 'generic', use_pynotifier=False, **options )
# set main thread and init thread pipe
kaa.notifier.set_current_as_mainthread()
# adding a timer or socket is not thread safe in general but
Modified: trunk/base/test/kaa_in_twisted.py
==============================================================================
--- trunk/base/test/kaa_in_twisted.py (original)
+++ trunk/base/test/kaa_in_twisted.py Mon Oct 29 14:28:26 2007
@@ -17,13 +17,14 @@
print 'kaa', kaa.notifier.is_mainthread()
# sys.exit(0)
-kaa.notifier.init('thread', handler = reactor.callFromThread)
+kaa.notifier.init('thread', handler = reactor.callFromThread, shutdown =
reactor.stop)
+# there is special code in kaa.notifier that does the same by calling
+# kaa.notifier.init('twisted')
reactor.callLater(2.5, twisted_callback1)
reactor.callLater(3.5, twisted_callback2)
kaa.notifier.Timer(kaa_callback).start(1)
-# you can either call notifier.main() or reactor.run()
reactor.run()
kaa.notifier.shutdown()
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog