Is it possible to wrap OpenSees interpreter / command line using python on Windows 10

Forum for asking and answering questions related to use of the OpenSeesPy module

Moderators: silvia, selimgunay, Moderators

Post Reply
MikeITExpert
Posts: 24
Joined: Sat Aug 21, 2021 9:19 pm

Is it possible to wrap OpenSees interpreter / command line using python on Windows 10

Post by MikeITExpert »

Hi All,

I have a large number of .tcl files containing model and analysis. I know I can convert .tcl files to OpenSeesPy; however it would be a time consuming and tedious task.

I am wondering if anybody tried to wrap Python around the OpenSees Command line Interface to automate some analysis task using Python.

I have tried using subproecss module, though I wasn't successful for some reason I don't get the command output from stdout (for the OpenSees Command line subprocess) as soon as I write commands to stdin.

Below is the code I am using:

Please share your ideas and experiences if any.

Regards,

Code: Select all

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)
Post Reply