move background function into BackgroundThread class
@@ -64,8 +64,10 @@ class InstructionQueue(queue.Queue):
class BackgroundThread(threading.Thread):
- def __init__(self, toBackground, fromBackground, *args, **keywords):
+ def __init__(self, programWindow, toBackground, fromBackground, *args,
+ **keywords):
threading.Thread.__init__(self, *args, **keywords)
+ self._programWindow = programWindow
self._toBackground = toBackground
self._fromBackground = fromBackground
@@ -73,28 +75,24 @@ class BackgroundThread(threading.Thread):
return self._fromBackground.get()
def hasMessage(self):
- # This thread yield (time.sleep(0)) dramatically improves Python3
- # performance. Without this line empty() will be called far too often.
- time.sleep(0)
return not self._fromBackground.empty()
def hasUserEvent(self):
- time.sleep(0) # See note in hasMessage().
return not self._toBackground.empty()
def put(self, instruction, message):
self._toBackground.put(instruction, message)
-
-def background(programWindow, inputQueue, outputQueue):
+ def run(self):
cmdCount = 0
block = True
pid = os.getpid()
signalNumber = signal.SIGUSR1
+ programWindow = self._programWindow
while True:
try:
try:
- instruction, message = inputQueue.get(block)
+ instruction, message = self._toBackground.get(block)
#profile = app.profile.beginPythonProfile()
if instruction == u"quit":
app.log.info('bg received quit message')
@@ -109,13 +107,13 @@ def background(programWindow, inputQueue, outputQueue):
# debugging only: programWindow.showWindowHierarchy()
cmdCount += len(message)
programWindow.program.backgroundFrame.setCmdCount(cmdCount)
- outputQueue.put(
+ self._fromBackground.put(
u"render",
programWindow.program.backgroundFrame.grabFrame())
os.kill(pid, signalNumber)
#app.profile.endPythonProfile(profile)
time.sleep(0) # See note in hasMessage().
- if block or not inputQueue.empty():
+ if block or not self._toBackground.empty():
continue
except queue.Empty:
pass
@@ -123,7 +121,7 @@ def background(programWindow, inputQueue, outputQueue):
if block:
programWindow.render()
programWindow.program.backgroundFrame.setCmdCount(cmdCount)
- outputQueue.put(
+ self._fromBackground.put(
u"render",
programWindow.program.backgroundFrame.grabFrame())
os.kill(pid, signalNumber)
@@ -132,24 +130,20 @@ def background(programWindow, inputQueue, outputQueue):
app.log.error('bg thread exception', e)
errorType, value, tracebackInfo = sys.exc_info()
out = traceback.format_exception(errorType, value, tracebackInfo)
- outputQueue.put(u"exception", out)
+ self._fromBackground.put(u"exception", out)
os.kill(pid, signalNumber)
while True:
- instruction, message = inputQueue.get()
+ instruction, message = self._toBackground.get()
if instruction == u"quit":
app.log.info('bg received quit message')
return
-
def startupBackground(programWindow):
toBackground = InstructionQueue()
fromBackground = InstructionQueue()
- bg = BackgroundThread(toBackground, fromBackground,
- target=background, args=(programWindow, toBackground, fromBackground))
+ bg = BackgroundThread(programWindow, toBackground, fromBackground)
bg.setName('ci_edit_bg')
bg.setDaemon(True)
bg.start()
- #bg.toBackground = toBackground
- #bg.fromBackground = fromBackground
return bg
