|
4 | 4 | [puget.printer :refer [cprint]]
|
5 | 5 | [clojure.inspector :as inspect :refer [inspect-tree]]
|
6 | 6 | [process-wrapper.util :refer :all]
|
| 7 | + [process-wrapper.parse :refer :all] |
7 | 8 | [me.raynes.conch.low-level :as sh]
|
8 | 9 | [instaparse.core :as insta]))
|
9 | 10 |
|
10 | 11 |
|
11 |
| -(def ^:private fasttext-output-parser |
12 |
| - (insta/parser |
13 |
| - "Labels = Label-Prediction (Space Label-Prediction)* |
| 12 | +(def worker-status (atom {})) ;; for now, only manages a single worker |
14 | 13 |
|
15 |
| - Label-Prediction = Label Space Probability |
16 |
| -
|
17 |
| - <Label> = <Label-Prefix> Label-Name |
18 |
| - <Label-Prefix> = '__label__' |
19 |
| - Label-Name = #'^\\S*' |
| 14 | +(defn attach [command-and-args] |
20 | 15 |
|
21 |
| - <Space> = <' '> |
| 16 | + " starts and returns a process ready to work with " |
22 | 17 |
|
23 |
| - <Probability> = Probability-Fraction | Probability-Fraction-Scientific-Notation | '0' | '1' |
| 18 | + (let |
| 19 | + [conch-object (apply sh/proc command-and-args) ; this is the map returned by `conch/sh`, consisting of the keys: :err :in :out :process |
| 20 | + process-object (:process conch-object) ; the java process object |
| 21 | + new-worker |
| 22 | + {process-object |
| 23 | + {:shell-object conch-object |
| 24 | + :status :idle}}] |
24 | 25 |
|
25 |
| - Probability-Fraction = #'0\\.[0-9]+' |
26 |
| - Probability-Fraction-Scientific-Notation = #'[0-9]+\\.[0-9]+e-[0-9]+'")) |
| 26 | + (swap! worker-status conj new-worker) |
27 | 27 |
|
| 28 | + new-worker)) |
28 | 29 |
|
29 |
| -(with-test |
30 |
| - (defn ^:private fasttext-output-parse [text] |
31 |
| - " parses fasttext output returned for a single text object " |
32 |
| - (let |
33 |
| - [parse-or-error (fasttext-output-parser text)] |
34 |
| - (if (insta/failure? parse-or-error) |
35 |
| - (do |
36 |
| - (println parse-or-error) |
37 |
| - (throw (Exception. (str "failed parsing what has been assumed to be fasttext prediction output: " text)))) |
38 |
| - parse-or-error))) |
39 |
| - ;; test code |
40 |
| - (is (= 1 1))) |
| 30 | +(defn ^:private get-worker-state [worker] |
| 31 | + (get-in (val (first worker)) [:status])) |
41 | 32 |
|
| 33 | +(add-watch worker-status :watcher |
| 34 | + (fn [watch-name watch-object old new] |
| 35 | + (println "watch activated") |
| 36 | + #_(do |
| 37 | + (println "changed from") |
| 38 | + (cprint old-state) |
| 39 | + (println "to") |
| 40 | + (cprint new-state)))) |
42 | 41 |
|
43 |
| -(defn get-prediction [process input] |
| 42 | +(defn get-prediction [worker input] |
44 | 43 |
|
45 | 44 | "pushes input text on stdin, gets the classification for it on stdout"
|
46 | 45 |
|
| 46 | + (swap! worker-status |
| 47 | + (fn swap-fn [worker-status] |
| 48 | + (update-in worker-status [(key (first worker)) :status] (constantly :working)))) |
| 49 | + |
47 | 50 | ; push the input (should end with a newline)
|
48 |
| - (sh/feed-from-string process input) |
| 51 | + |
| 52 | + (sh/feed-from-string (get-in (val (first worker)) [:shell-object]) input) |
49 | 53 |
|
50 | 54 | ; get the output prediction
|
51 | 55 | (let
|
52 | 56 | [rawparse-or-error
|
53 | 57 | (loop [raw-response (str)]
|
54 | 58 | (let
|
55 |
| - [byte-read (.read (:out process))] |
| 59 | + [byte-read (.read (get-in (val (first worker)) [:shell-object :out]))] |
56 | 60 | (assert (not= byte-read -1))
|
57 | 61 | (let [as-char (char byte-read)]
|
58 | 62 | (if (= byte-read 10) ; line-feed
|
59 | 63 | raw-response
|
60 |
| - (recur (str raw-response as-char))))))] |
| 64 | + (recur (str raw-response as-char)))))) |
61 | 65 |
|
62 |
| - (fasttext-output-parse rawparse-or-error))) |
63 |
| - |
64 |
| - |
65 |
| -(defn attach [command-and-args] |
66 |
| - |
67 |
| - " starts and returns a process ready to work with " |
| 66 | + parse (fasttext-output-parse rawparse-or-error)] |
68 | 67 |
|
69 |
| - (apply sh/proc command-and-args)) |
| 68 | + (swap! worker-status |
| 69 | + (fn [process-list] |
| 70 | + (update-in process-list [(key (first worker)) :status] (constantly :idle)))) |
70 | 71 |
|
| 72 | + parse)) |
71 | 73 |
|
72 | 74 |
|
73 | 75 |
|
0 commit comments