@@ -116,7 +116,7 @@ class WorkerThread(TerminatableThread):
116116 t[1] = optional, tuple or list of arguments to pass to the routine
117117 t[2] = optional, dictionary of keyword arguments to pass to the routine
118118 """
119- __slots__ = ('inq' , '_current_routine' )
119+ __slots__ = ('inq' )
120120
121121
122122 # define how often we should check for a shutdown request in case our
@@ -128,7 +128,6 @@ def __init__(self, inq = None):
128128 self .inq = inq
129129 if inq is None :
130130 self .inq = Queue .Queue ()
131- self ._current_routine = None # routine we execute right now
132131
133132 @classmethod
134133 def stop (cls , * args ):
@@ -141,7 +140,6 @@ def run(self):
141140
142141 gettask = self .inq .get
143142 while True :
144- self ._current_routine = None
145143 if self ._should_terminate ():
146144 break
147145 # END check for stop request
@@ -153,35 +151,38 @@ def run(self):
153151 assert len (tasktuple ) == 2 , "Need tuple of function, arg - it could be more flexible, but its reduced to what we need"
154152 routine , arg = tasktuple
155153
156- self ._current_routine = routine
157-
158154 try :
159- rval = None
160- if inspect .ismethod (routine ):
161- if routine .im_self is None :
162- rval = routine (self , arg )
163- else :
155+ try :
156+ rval = None
157+ if inspect .ismethod (routine ):
158+ if routine .im_self is None :
159+ rval = routine (self , arg )
160+ else :
161+ rval = routine (arg )
162+ elif inspect .isroutine (routine ):
164163 rval = routine (arg )
165- elif inspect .isroutine (routine ):
166- rval = routine (arg )
167- else :
168- # ignore unknown items
169- print >> sys .stderr , "%s: task %s was not understood - terminating" % (self .getName (), str (tasktuple ))
170- break
171- # END make routine call
164+ else :
165+ # ignore unknown items
166+ print >> sys .stderr , "%s: task %s was not understood - terminating" % (self .getName (), str (tasktuple ))
167+ break
168+ # END make routine call
169+ finally :
170+ # make sure we delete the routine to release the reference as soon
171+ # as possible. Otherwise objects might not be destroyed
172+ # while we are waiting
173+ del (routine )
174+ del (tasktuple )
172175 except StopProcessing :
173176 print self .name , "stops processing" # DEBUG
174177 break
175178 except Exception ,e :
176179 print >> sys .stderr , "%s: Task %s raised unhandled exception: %s - this really shouldn't happen !" % (self .getName (), str (tasktuple ), str (e ))
177180 continue # just continue
178181 # END routine exception handling
182+
183+ # END handle routine release
179184 # END endless loop
180185
181- def routine (self ):
182- """:return: routine we are currently executing, or None if we have no task"""
183- return self ._current_routine
184-
185186 def stop_and_join (self ):
186187 """Send stop message to ourselves"""
187188 self .inq .put ((self .stop , None ))
0 commit comments