-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbc_thread_class.py
More file actions
29 lines (22 loc) · 800 Bytes
/
bc_thread_class.py
File metadata and controls
29 lines (22 loc) · 800 Bytes
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
from threading import Thread
import time
from subprocess import Popen, STDOUT, PIPE
class ProcessOutputThread(Thread):
def __init__(self, process):
Thread.__init__(self)
self.process = process
def run(self):
while self.process.poll() is None: #To make sure the process is alive.
print(self.process.stdout.readline().decode(), end="") #Non Blocking IO
p = Popen(['bc', '-i'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
t = ProcessOutputThread(p)
t.start() #This calls t.run() and executes it by not blocking the main IO
while p.poll() is None: #To make sure the process is alive.
query = input()
if query == 'quit' or query == 'exit':
p.communicate(query.encode(), timeout=1)
if p.poll() is not None:
break
query = query + '\n'
p.stdin.write(query.encode())
p.stdin.flush()