-
Notifications
You must be signed in to change notification settings - Fork 2
/
runthis.py
144 lines (118 loc) · 3.93 KB
/
runthis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# two_subprocesses_with_output_and_input.py
import subprocess
import threading
import sys
import time
ready = False
first = True
try:
import queue
except ImportError:
import Queue as queue
def read_output(pipe, q):
"""reads output from `pipe`, when line has been read, puts
line on Queue `q`"""
while True:
#while True:
l = pipe.read(2)
if l:
#sys.stdout.write("!!!!!!!!!!!!!!!!!")
#sys.stdout.write("\n")
#sys.stdout.write(l.decode('UTF-8'))
#sys.stdout.flush()
q.put(l)
#x = q.empty()
#sys.stdout.write(str(x))
#sys.stdout.flush()
#print l,
else:
#print("HER")
break
#time.sleep(.01)
def write_to_subproc(write_pipe,message):
"""reads input from a pipe with name `read_pipe_name`,
writing this input straight into `write_pipe`"""
write_pipe.write(message.encode('UTF-8'))
write_pipe.flush()
#while True:
#sys.stdout.write("!")
#sys.stdout.flush()
#write_pipe.write('a'.encode('UTF-8'))
#write_pipe.flush()
#time.sleep(3)
'''with open(in_pipe_name, "r") as f:
x = f.read()
write_pipe.write(x.encode('UTF-8'))
sys.stdout.write(x)
sys.stdout.flush()
'''
# start both `proc_a.py` and `proc_b.py`
proc_a = subprocess.Popen(["stdbuf", "-o0", "python3", "classify_image.py"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc_b = subprocess.Popen(["stdbuf", "-o0", "python3", "app.py"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# lists for storing the lines of output generated
pa_line_buffer = []
pb_line_buffer = []
# queues for storing output lines
pa_q = queue.Queue()
pb_q = queue.Queue()
# start a pair of threads to read output from procedures A and B
pa_t = threading.Thread(target=read_output, args=(proc_a.stdout, pa_q))
pb_t = threading.Thread(target=read_output, args=(proc_b.stdout, pb_q))
pa_t.daemon = True
pb_t.daemon = True
pa_t.start()
pb_t.start()
'''
# start a pair of threads to read input into procedures A and B
pa_input_thread = threading.Thread(target=read_input, args=(proc_a.stdin, "proc_a_input"))
pb_input_thread = threading.Thread(target=read_input, args=(proc_b.stdin, "proc_b_input"))
pa_input_thread.daemon = True
pb_input_thread.daemon = True
pa_input_thread.start()
pb_input_thread.start()
'''
global ready
while True:
# check if either sub-process has finished
proc_a.poll()
proc_b.poll()
if proc_a.returncode is not None or proc_b.returncode is not None:
break
# write output from procedure A (if there is any)
try:
l = pa_q.get(False)
#sys.stdout.write("A: ")
received = l.decode('UTF-8')
sys.stdout.write(received)
sys.stdout.flush()
if ready:
sys.stdout.write('!')
sys.stdout.flush()
write_to_subproc(proc_b.stdin,received)
if received == '{{':
sys.stdout.write("\nrecognition done\n")
sys.stdout.flush()
#write_to_subproc(proc_b.stdin,"an")
ready = True
except queue.Empty:
#sys.stdout.write("QUEUE IS EMPTY")
pass
# write output from procedure B (if there is any)
try:
b = pb_q.get(False)
#sys.stdout.write("B: ")
#sys.stdout.write(l.decode('UTF-8'))
received = b.decode('UTF-8')
sys.stdout.write(received)
sys.stdout.flush()
if received == 'qq':
received = ''
sys.stdout.write("1")
sys.stdout.write("yaaayB")
sys.stdout.write("\n")
sys.stdout.flush()
write_to_subproc(proc_a.stdin,"re")
except queue.Empty:
pass