def _thread_wrapper(self, *args):
''' Wrapper for the worker method defined in the module. Handles calling the actual worker, cleanly exiting upon
interrupt, and passing exceptions back to the main process.'''
thread_name = threading.current_thread().name
self.debug('THREAD => %s started.' % thread_name)
while not self.stopped.is_set():
try:
# use the get_nowait() method for retrieving a queued item to
# prevent the thread from blocking when the queue is empty
obj = self.q.get_nowait()
except Empty:
continue
try:
# launch the public module_thread method
self.module_thread(obj, *args)
except:
# handle exceptions local to the thread
self.print_exception('(thread=%s, object=%s)' % (thread_name, repr(obj)))
finally:
self.q.task_done()
self.debug('THREAD => %s exited.' % thread_name)
# sometimes a keyboardinterrupt causes a race condition between when the self.q.task_done() call above and the
# self.q.empty() call below, causing all the threads to hang. introducing the time.sleep(.7) call below reduces
# the likelihood of encountering the race condition.
评论列表
文章目录