From 41a60920e413a513801ba186de86e2a7fd7ef9c3 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Wed, 2 Oct 2019 16:29:10 +0200 Subject: text: Fail if workers die --- lulua/text.py | 68 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/lulua/text.py b/lulua/text.py index f0a1b3b..98c7824 100644 --- a/lulua/text.py +++ b/lulua/text.py @@ -179,39 +179,44 @@ charMap = { } def writeWorker (args, inq, outq): - keyboard = defaultKeyboards['ibmpc105'] - layout = defaultLayouts['null'].specialize (keyboard) - w = Writer (layout) - combined = dict ((cls.name, cls(w)) for cls in allStats) - - while True: - keyboard = defaultKeyboards[args.keyboard] - layout = defaultLayouts[args.layout].specialize (keyboard) + try: + keyboard = defaultKeyboards['ibmpc105'] + layout = defaultLayouts['null'].specialize (keyboard) w = Writer (layout) + combined = dict ((cls.name, cls(w)) for cls in allStats) + + while True: + keyboard = defaultKeyboards[args.keyboard] + layout = defaultLayouts[args.layout].specialize (keyboard) + w = Writer (layout) + + item = inq.get () + if item is None: + break + + # extract + text = sources[args.source] (item) + text = ''.join (map (lambda x: charMap.get (x, x), text)) + # XXX sanity checks, disable + for c in charMap.keys (): + if c in text: + #print (c, 'is in text', file=sys.stderr) + assert False, c + + # stats + stats = [cls(w) for cls in allStats] + for match, event in w.type (StringIO (text)): + for s in stats: + s.process (event) - item = inq.get () - if item is None: - break - - # extract - text = sources[args.source] (item) - text = ''.join (map (lambda x: charMap.get (x, x), text)) - # XXX sanity checks, disable - for c in charMap.keys (): - if c in text: - #print (c, 'is in text', file=sys.stderr) - assert False, c - - # stats - stats = [cls(w) for cls in allStats] - for match, event in w.type (StringIO (text)): for s in stats: - s.process (event) - - for s in stats: - combined[s.name].update (s) + combined[s.name].update (s) - outq.put (combined) + outq.put (combined) + except Exception as e: + # async exceptions + outq.put (None) + raise def write (): """ Extract corpus source file, convert to plain text, map chars and create stats """ @@ -244,6 +249,10 @@ def write (): for l in sys.stdin: inq.put (l) bar.update (n=1) + + # something is wrong + if not outq.empty (): + return 1 except KeyboardInterrupt: pass @@ -257,4 +266,5 @@ def write (): for w in workers: w.join () + return 0 -- cgit v1.2.3