Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

A posible workaround could be using the multiprocessing module in this way:

import multiprocessing

class psspy_worker(multiprocessing.Process):

    def __init__(self, pipe_conn):
        multiprocessing.Process.__init__(self)
        self.pipe = pipe_conn

    def run(self):
        import psse34
        import psspy

        while True:
            msg = self.pipe.recv()
            command = msg.get("command")

            if command == "kill":
                break

            if command == "call":
                func_name = msg.get("func_name")
                args = msg.get("args", [])
                kwargs = msg.get("kwargs", {})
                func = getattr(psspy, func_name)
                result = func(*args, **kwargs)
                self.pipe.send({"status": "success", "result": result})


class isolated_psspy(object):

    def __init__(self):
        self.parent_conn, self.child_conn = multiprocessing.Pipe()
        self.worker = psspy_worker(self.child_conn)
        self.worker.start()


    def __getattr__(self, name):
        if name.startswith("__"):
            raise AttributeError(name)

        def wrapper(*args, **kwargs):
            self.parent_conn.send({
                "command": "call",
                "func_name": name,
                "args": args,
                "kwargs": kwargs,
                })
            response = self.parent_conn.recv()
            return response["result"]

        return wrapper


    def close(self):
        self.parent_conn.send({"command": "kill"})
        self.worker.join()


if __name__ == "__main__":

    psspy1 = isolated_psspy()
    psspy2 = isolated_psspy()

    psspy1.psseinit()
    psspy2.psseinit()

    psspy1.case("case1.sav")
    psspy2.case("case2.sav")

    line11, _ = psspy1.titldt()
    line12, _ = psspy2.titldt()

    assert line11 != line12