summaryrefslogtreecommitdiff
path: root/realtime/rt/decoder.py
diff options
context:
space:
mode:
authorMichael Denkowski <mdenkows@cs.cmu.edu>2013-09-25 16:20:51 -0700
committerMichael Denkowski <mdenkows@cs.cmu.edu>2013-09-25 16:20:51 -0700
commitbd2fe67ac2e2f7c22bf279aeef5439820329e6dc (patch)
tree5cbf8f8d45b84f383504941e02a844df46d19985 /realtime/rt/decoder.py
parent17497f2e77e63e6aa549eedc279cac46cfd25e2b (diff)
Super multi-user thread safety update
Diffstat (limited to 'realtime/rt/decoder.py')
-rw-r--r--realtime/rt/decoder.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/realtime/rt/decoder.py b/realtime/rt/decoder.py
index aa6db64d..72b5b959 100644
--- a/realtime/rt/decoder.py
+++ b/realtime/rt/decoder.py
@@ -1,27 +1,37 @@
import logging
import os
import subprocess
+import threading
import util
class Decoder:
- def close(self):
+ def close(self, force=False):
+ if not force:
+ self.semaphore.acquire()
self.decoder.stdin.close()
+ if not force:
+ self.semaphore.release()
def decode(self, sentence, grammar=None):
+ '''Threadsafe'''
input = '<seg grammar="{g}">{s}</seg>\n'.format(s=sentence, g=grammar) if grammar else '{}\n'.format(sentence)
+ self.semaphore.acquire()
self.decoder.stdin.write(input)
- return self.decoder.stdout.readline().strip()
+ hyp = self.decoder.stdout.readline().strip()
+ self.semaphore.release()
+ return hyp
class CdecDecoder(Decoder):
-
+
def __init__(self, config, weights):
cdec_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
decoder = os.path.join(cdec_root, 'decoder', 'cdec')
decoder_cmd = [decoder, '-c', config, '-w', weights]
logging.info('Executing: {}'.format(' '.join(decoder_cmd)))
self.decoder = util.popen_io(decoder_cmd)
+ self.semaphore = threading.Semaphore()
class MIRADecoder(Decoder):
@@ -32,15 +42,27 @@ class MIRADecoder(Decoder):
mira_cmd = [mira, '-c', config, '-w', weights, '-o', '2', '-C', '0.001', '-b', '500', '-k', '500', '-u', '-t']
logging.info('Executing: {}'.format(' '.join(mira_cmd)))
self.decoder = util.popen_io(mira_cmd)
+ self.semaphore = threading.Semaphore()
def get_weights(self):
+ '''Threadsafe'''
+ self.semaphore.acquire()
self.decoder.stdin.write('WEIGHTS ||| WRITE\n')
- return self.decoder.stdout.readline().strip()
+ weights = self.decoder.stdout.readline().strip()
+ self.semaphore.release()
+ return weights
def set_weights(self, w_line):
+ '''Threadsafe'''
+ self.semaphore.acquire()
self.decoder.stdin.write('WEIGHTS ||| {}\n'.format(w_line))
+ self.semaphore.release()
def update(self, sentence, grammar, reference):
+ '''Threadsafe'''
input = 'LEARN ||| <seg grammar="{g}">{s}</seg> ||| {r}\n'.format(s=sentence, g=grammar, r=reference)
+ self.semaphore.acquire()
self.decoder.stdin.write(input)
- return self.decoder.stdout.readline().strip()
+ log = self.decoder.stdout.readline().strip()
+ self.semaphore.release()
+ return log