Author: dmeyer
Date: Sun Oct 28 14:54:37 2007
New Revision: 2885

Log:
Add small KaaReactor code to use kaa.notifier with Twisted


Added:
   trunk/base/src/notifier/reactor.py
   trunk/base/test/twisted_integration.py

Added: trunk/base/src/notifier/reactor.py
==============================================================================
--- (empty file)
+++ trunk/base/src/notifier/reactor.py  Sun Oct 28 14:54:37 2007
@@ -0,0 +1,86 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# reactor.py - Twisted integration code
+# -----------------------------------------------------------------------------
+# $Id: async.py 2790 2007-08-31 18:34:55Z dmeyer $
+#
+# -----------------------------------------------------------------------------
+# kaa.notifier - Mainloop and callbacks
+# Copyright (C) 2007 Dirk Meyer, Jason Tackaberry, et al.
+#
+# First Version: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#
+# Please see the file AUTHORS for a complete list of authors.
+#
+# This library is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version
+# 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+#
+# -----------------------------------------------------------------------------
+
+# get and install reactor
+from twisted.internet import threadedselectreactor
+
+import kaa.notifier
+
+class KaaReactor(threadedselectreactor.ThreadedSelectReactor):
+    """
+    Twisted reactor for kaa.notifier.
+    """
+    def _kaa_callback(self, func):
+        """
+        Callback from the Twisted thread kaa should execute from
+        the mainloop.
+        """
+        a = kaa.notifier.MainThreadCallback(func)
+        a.set_async(True)
+        return a()
+
+
+    def _kaa_stop(self):
+        """
+        Callback when Twisted wants to stop.
+        """
+        if not kaa.notifier.is_mainthread():
+            return kaa.notifier.MainThreadCallback(twisted_stop)()
+        kaa.notifier.OneShotTimer(kaa.notifier.shutdown).start(0)
+        kaa.notifier.signals['shutdown'].disconnect(self.stop)
+
+
+    def connect(self):
+        """
+        Connect the reactor to kaa.notifier.
+        """
+        self.interleave(self._kaa_callback)
+        self.addSystemEventTrigger('after', 'shutdown', self._kaa_stop)
+        kaa.notifier.signals['shutdown'].connect(self.stop)
+
+
+    def run(self, installSignalHandlers=1):
+        """
+        Run the reactor by starting the notifier mainloop.
+        """
+        self.startRunning(installSignalHandlers=installSignalHandlers)
+        kaa.notifier.loop()
+
+
+def install():
+    """
+    Configure the twisted mainloop to be run using the kaa reactor.
+    """
+    reactor = KaaReactor()
+    from twisted.internet.main import installReactor
+    installReactor(reactor)
+    reactor.connect()
+    return reactor

Added: trunk/base/test/twisted_integration.py
==============================================================================
--- (empty file)
+++ trunk/base/test/twisted_integration.py      Sun Oct 28 14:54:37 2007
@@ -0,0 +1,30 @@
+import sys
+import kaa
+
+# install special kaa reactor
+import kaa.notifier.reactor
+kaa.notifier.reactor.install()
+
+# get reactor
+from twisted.internet import reactor
+
+def twisted_callback1():
+    print "twisted", kaa.notifier.is_mainthread()
+    
+def twisted_callback2():
+    print "twisted (shutdown)", kaa.notifier.is_mainthread()
+    reactor.stop()
+    
+def kaa_callback():
+    print 'kaa', kaa.notifier.is_mainthread()
+    # sys.exit(0)
+    
+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.main()
+
+print 'stop'

-------------------------------------------------------------------------
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

Reply via email to