I am trying to wrap OpenSees CLI in Python 3.8 (Windows 10) and I tried couple of approaches from SO, though none helped.
I tired this: Running an interactive command from within Python which seemed a pretty popular approach but didn't work so far.
Below is the code. Please let me know your comments.
import sys
import subprocess
from threading import Thread
from queue import Queue, Empty # Python 3.x
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
def getOutput(outQueue):
outStr = ''
try:
while True: # Adds output from the Queue until it is empty
outStr+=outQueue.get_nowait()
except Empty:
return outStr
p = subprocess.Popen("bin\\OpenSees.exe",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False, universal_newlines=True)
outQueue = Queue()
errQueue = Queue()
outThread = Thread(target=enqueue_output, args=(p.stdout, outQueue))
errThread = Thread(target=enqueue_output, args=(p.stderr, errQueue))
outThread.daemon = True
errThread.daemon = True
outThread.start()
errThread.start()
for i in range(3):
try:
someInput = raw_input("Input: ")
except NameError:
someInput = input("Input: ")
p.stdin.write(someInput+"\n")
errors = getOutput(errQueue)
output = getOutput(outQueue)
print(errors, output)