Author: dmeyer
Date: Fri Jan 18 14:27:13 2008
New Revision: 2980

Log:
create new ThreadCallback

Modified:
   trunk/base/src/notifier/__init__.py
   trunk/base/src/notifier/jobserver.py
   trunk/base/src/notifier/thread.py
   trunk/base/test/asynctest.py

Modified: trunk/base/src/notifier/__init__.py
==============================================================================
--- trunk/base/src/notifier/__init__.py (original)
+++ trunk/base/src/notifier/__init__.py Fri Jan 18 14:27:13 2008
@@ -32,7 +32,7 @@
 
 from popen import Process
 from callback import Callback, WeakCallback, Signal, Signals
-from thread import MainThreadCallback, Thread, is_mainthread
+from thread import MainThreadCallback, ThreadCallback, Thread, is_mainthread
 from timer import Timer, WeakTimer, OneShotTimer, WeakOneShotTimer, AtTimer, 
OneShotAtTimer
 from sockets import SocketDispatcher, WeakSocketDispatcher, Socket, IO_READ, 
IO_WRITE
 from event import Event, EventHandler, WeakEventHandler
@@ -48,7 +48,7 @@
     'OneShotAtTimer', 'SocketDispatcher', 'WeakSocketDispatcher', 'Socket',
     'IO_READ', 'IO_WRITE', 'Event', 'EventHandler', 'WeakEventHandler',
     'YieldContinue', 'YieldCallback', 'YieldFunction', 'NamedThreadCallback',
-    'InProgress',
+    'InProgress', 'ThreadCallback',
 
     # decorator for sub modules
     # FIXME: while we are breaking the API right now, do we want to keep

Modified: trunk/base/src/notifier/jobserver.py
==============================================================================
--- trunk/base/src/notifier/jobserver.py        (original)
+++ trunk/base/src/notifier/jobserver.py        Fri Jan 18 14:27:13 2008
@@ -59,9 +59,9 @@
         def newfunc(*args, **kwargs):
             if name:
                 return NamedThreadCallback((name, priority), func, *args, 
**kwargs)()
-            t = thread.Thread(func, *args, **kwargs)
+            t = thread.ThreadCallback(func, *args, **kwargs)
             t.wait_on_exit(False)
-            return t.start()
+            return t()
 
         try:
             newfunc.func_name = func.func_name

Modified: trunk/base/src/notifier/thread.py
==============================================================================
--- trunk/base/src/notifier/thread.py   (original)
+++ trunk/base/src/notifier/thread.py   Fri Jan 18 14:27:13 2008
@@ -46,7 +46,8 @@
 #
 # -----------------------------------------------------------------------------
 
-__all__ = [ 'MainThreadCallback', 'Thread', 'is_mainthread', 'wakeup', 
'set_as_mainthread' ]
+__all__ = [ 'MainThreadCallback', 'ThreadCallback', 'Thread', 'is_mainthread',
+            'wakeup', 'set_as_mainthread' ]
 
 # python imports
 import sys
@@ -163,6 +164,46 @@
         self._callback = None
 
 
+class ThreadCallback(Callback):
+    """
+    Notifier aware wrapper for threads. When a thread is started, it is
+    impossible to fork the current process into a second one without exec both
+    using the notifier main loop because of the shared _thread_notifier_pipe.
+    """
+    _daemon = False
+    
+    def wait_on_exit(self, wait=False):
+        """
+        Wait for the thread on application exit. Default is True.
+        """
+        self._daemon = not wait
+
+
+    def _create_thread(self, *args, **kwargs):
+        """
+        Create and start the thread.
+        """
+        cb = Callback._get_callback(self)
+        async = ThreadInProgress(cb, *args, **kwargs)
+        # create thread and setDaemon
+        t = threading.Thread(target=async._execute)
+        t.setDaemon(self._daemon)
+        # connect thread.join to the InProgress
+        async.connect(t.join)
+        async.exception_handler.connect(t.join)
+        # start the thread
+        t.start()
+        return async
+
+
+    def _get_callback(self):
+        """
+        Return callable for this Callback.
+        """
+        return self._create_thread
+
+
+    
 class Thread(threading.Thread):
     """
     Notifier aware wrapper for threads. When a thread is started, it is

Modified: trunk/base/test/asynctest.py
==============================================================================
--- trunk/base/test/asynctest.py        (original)
+++ trunk/base/test/asynctest.py        Fri Jan 18 14:27:13 2008
@@ -49,7 +49,7 @@
 def thread(x):
     return x + 1 - 1
 
[EMAIL PROTECTED]('foo')
[EMAIL PROTECTED]()
 def thread2(c, x):
     # call rpc in thread using MainThreadCallback
     cb = kaa.MainThreadCallback(c.rpc)

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