@@ -136,16 +136,21 @@ def stop(cls, *args):
136
136
137
137
def run (self ):
138
138
"""Process input tasks until we receive the quit signal"""
139
- print self .name , "starts processing" # DEBUG
140
-
141
139
gettask = self .inq .get
142
140
while True :
143
141
if self ._should_terminate ():
144
142
break
145
143
# END check for stop request
146
144
147
- # we wait and block - to terminate, send the 'stop' method
145
+ # note: during shutdown, this turns None in the middle of waiting
146
+ # for an item to be put onto it - we can't du anything about it -
147
+ # even if we catch everything and break gracefully, the parent
148
+ # call will think we failed with an empty exception.
149
+ # Hence we just don't do anything about it. Alternatively
150
+ # we could override the start method to get our own bootstrapping,
151
+ # which would mean repeating plenty of code in of the threading module.
148
152
tasktuple = gettask ()
153
+
149
154
# needing exactly one function, and one arg
150
155
routine , arg = tasktuple
151
156
@@ -161,7 +166,7 @@ def run(self):
161
166
rval = routine (arg )
162
167
else :
163
168
# ignore unknown items
164
- print >> sys .stderr , "%s: task %s was not understood - terminating" % (self .getName (), str (tasktuple ))
169
+ sys .stderr . write ( "%s: task %s was not understood - terminating\n " % (self .getName (), str (tasktuple ) ))
165
170
break
166
171
# END make routine call
167
172
finally :
@@ -171,18 +176,22 @@ def run(self):
171
176
del (routine )
172
177
del (tasktuple )
173
178
except StopProcessing :
174
- print self .name , "stops processing" # DEBUG
175
179
break
176
180
except Exception ,e :
177
- print >> sys .stderr , "%s: Task %s raised unhandled exception: %s - this really shouldn't happen !" % (self .getName (), str (tasktuple ), str (e ))
181
+ sys .stderr . write ( "%s: Task %s raised unhandled exception: %s - this really shouldn't happen !\n " % (self .getName (), str (tasktuple ), str (e ) ))
178
182
continue # just continue
179
183
# END routine exception handling
180
184
181
185
# END handle routine release
182
186
# END endless loop
183
187
184
188
def stop_and_join (self ):
185
- """Send stop message to ourselves"""
189
+ """Send stop message to ourselves - we don't block, the thread will terminate
190
+ once it has finished processing its input queue to receive our termination
191
+ event"""
192
+ # DONT call superclass as it will try to join - join's don't work for
193
+ # some reason, as python apparently doesn't switch threads (so often)
194
+ # while waiting ... I don't know, but the threads respond properly,
195
+ # but only if dear python switches to them
186
196
self .inq .put ((self .stop , None ))
187
- super (WorkerThread , self ).stop_and_join ()
188
197
#} END classes
0 commit comments