fix possible race in genericDriver

This commit is contained in:
chrys 2017-05-06 13:04:06 +02:00
parent c13a4d554e
commit 2e15a2a5ba

View File

@ -6,10 +6,9 @@
# generic driver # generic driver
from core import debug from core import debug
from threading import Thread from threading import Thread, Lock
from queue import Queue, Empty from queue import Queue, Empty
import time from subprocess import Popen
from subprocess import Popen, PIPE
class speakQueue(Queue): class speakQueue(Queue):
def clear(self): def clear(self):
@ -23,6 +22,7 @@ class driver():
def __init__(self): def __init__(self):
self.proc = None self.proc = None
self.speechThread = Thread(target=self.worker) self.speechThread = Thread(target=self.worker)
self.lock = Lock()
self.textQueue = speakQueue() self.textQueue = speakQueue()
self.volume = '' self.volume = ''
self.rate = '' self.rate = ''
@ -74,6 +74,7 @@ class driver():
if not self._isInitialized: if not self._isInitialized:
return return
self.clear_buffer() self.clear_buffer()
self.lock.acquire(True)
if self.proc: if self.proc:
try: try:
self.proc.terminate() self.proc.terminate()
@ -81,7 +82,7 @@ class driver():
self.proc.kill() self.proc.kill()
finally: finally:
self.proc = None self.proc = None
self.lock.release()
def setCallback(self, callback): def setCallback(self, callback):
print('SpeechDummyDriver: setCallback') print('SpeechDummyDriver: setCallback')
@ -149,12 +150,13 @@ class driver():
popenSpeechCommand = popenSpeechCommand.replace('fenrirText', str(utterance['text']).replace('"','').replace('\n','')) popenSpeechCommand = popenSpeechCommand.replace('fenrirText', str(utterance['text']).replace('"','').replace('\n',''))
try: try:
s = time.time() self.lock.acquire(True)
self.proc = Popen(popenSpeechCommand, shell=True) self.proc = Popen(popenSpeechCommand, shell=True)
self.proc.wait() self.lock.release()
print(popenSpeechCommand) self.proc.wait() # critical for MT? - if we lock it we deadlock it maybe.
print('run',time.time() -s)
except Exception as e: except Exception as e:
print('except' + str(e)) self.env['runtime']['debug'].writeDebugOut('speechDriver:worker:'str(e),debug.debugLevel.ERROR)
self.lock.acquire(True)
self.proc = None self.proc = None
self.lock.release()