[Zodb-checkins] SVN: ZODB/trunk/src/ZEO/zrpc/trigger.py Clean up thunk management logic.

Jim Fulton jim at zope.com
Wed Jan 27 11:05:55 EST 2010


Log message for revision 108569:
  Clean up thunk management logic.
  
  Allow thunks to take args.
  
  Don't hold trigger lock when calling thunks.
  

Changed:
  U   ZODB/trunk/src/ZEO/zrpc/trigger.py

-=-
Modified: ZODB/trunk/src/ZEO/zrpc/trigger.py
===================================================================
--- ZODB/trunk/src/ZEO/zrpc/trigger.py	2010-01-27 14:02:55 UTC (rev 108568)
+++ ZODB/trunk/src/ZEO/zrpc/trigger.py	2010-01-27 16:05:55 UTC (rev 108569)
@@ -12,6 +12,8 @@
 #
 ##############################################################################
 
+from __future__ import with_statement
+
 import asyncore
 import os
 import socket
@@ -95,14 +97,15 @@
     def _close(self):    # see close() above; subclass must supply
         raise NotImplementedError
 
-    def pull_trigger(self, thunk=None):
+    def pull_trigger(self, *thunk):
         if thunk:
-            self.lock.acquire()
-            try:
+            with self.lock:
                 self.thunks.append(thunk)
-            finally:
-                self.lock.release()
-        self._physical_pull()
+        try:
+            self._physical_pull()
+        except Exception:
+            if not self._closed:
+                raise
 
     # Subclass must supply _physical_pull, which does whatever the OS
     # needs to do to provoke the "write" end of the trigger.
@@ -114,19 +117,20 @@
             self.recv(8192)
         except socket.error:
             return
-        self.lock.acquire()
-        try:
-            for thunk in self.thunks:
-                try:
-                    thunk()
-                except:
-                    nil, t, v, tbinfo = asyncore.compact_traceback()
-                    print ('exception in trigger thunk:'
-                           ' (%s:%s %s)' % (t, v, tbinfo))
-            self.thunks = []
-        finally:
-            self.lock.release()
 
+        while 1:
+            with self.lock:
+                if self.thunks:
+                    thunk = self.thunks.pop(0)
+                else:
+                    return
+            try:
+                thunk[0](*thunk[1:])
+            except:
+                nil, t, v, tbinfo = asyncore.compact_traceback()
+                print ('exception in trigger thunk:'
+                       ' (%s:%s %s)' % (t, v, tbinfo))
+
     def __repr__(self):
         return '<select-trigger (%s) at %x>' % (self.kind, positive_id(self))
 



More information about the Zodb-checkins mailing list