Rework threadutil to use Queue for simplification and power-friendliness

Removes code and should avoid weird Python situation that causes cpu
wakeups

fixes deb#434074
fixes #66
This commit is contained in:
John Goerzen 2008-08-02 15:10:11 -05:00
parent 2b23657db0
commit 8114877fae

View File

@ -18,6 +18,7 @@
from threading import * from threading import *
from StringIO import StringIO from StringIO import StringIO
from Queue import Queue
import sys, traceback, thread import sys, traceback, thread
from offlineimap.ui import UIBase # for getglobalui() from offlineimap.ui import UIBase # for getglobalui()
@ -88,8 +89,7 @@ class threadlist:
# Exit-notify threads # Exit-notify threads
###################################################################### ######################################################################
exitcondition = Condition(Lock()) exitthreads = Queue(5)
exitthreads = []
inited = 0 inited = 0
def initexitnotify(): def initexitnotify():
@ -110,17 +110,10 @@ def exitnotifymonitorloop(callback):
an ExitNotifyThread, or else an infinite loop may result. Furthermore, an ExitNotifyThread, or else an infinite loop may result. Furthermore,
the monitor will hold the lock all the while the other thread is waiting. the monitor will hold the lock all the while the other thread is waiting.
""" """
global exitcondition, exitthreads global exitthreads
while 1: # Loop forever. while 1: # Loop forever.
exitcondition.acquire() callback(exitthreads.get(True))
try: exitthreads.task_done()
while not len(exitthreads):
exitcondition.wait(1)
while len(exitthreads):
callback(exitthreads.pop(0)) # Pull off in order added!
finally:
exitcondition.release()
def threadexited(thread): def threadexited(thread):
"""Called when a thread exits.""" """Called when a thread exits."""
@ -146,7 +139,7 @@ class ExitNotifyThread(Thread):
"""This class is designed to alert a "monitor" to the fact that a thread has """This class is designed to alert a "monitor" to the fact that a thread has
exited and to provide for the ability for it to find out why.""" exited and to provide for the ability for it to find out why."""
def run(self): def run(self):
global exitcondition, exitthreads, profiledir global exitthreads, profiledir
self.threadid = thread.get_ident() self.threadid = thread.get_ident()
try: try:
if not profiledir: # normal case if not profiledir: # normal case
@ -171,10 +164,8 @@ class ExitNotifyThread(Thread):
self.setExitCause('NORMAL') self.setExitCause('NORMAL')
if not hasattr(self, 'exitmessage'): if not hasattr(self, 'exitmessage'):
self.setExitMessage(None) self.setExitMessage(None)
exitcondition.acquire()
exitthreads.append(self) exitthreads.put(self, True)
exitcondition.notify()
exitcondition.release()
def setExitCause(self, cause): def setExitCause(self, cause):
self.exitcause = cause self.exitcause = cause