Ask Your Question
0

Activate PSSE License from Python

asked 2012-09-18 17:50:28 -0600

JervisW gravatar image

Is it possible to control the activation of the PSSE license from within Python?

I'm running a program that doesn't need to use PSSE much, and it seems like a waste to use the hourly license up for the time that PSSE isn't required.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-09-19 06:55:36 -0600

jsexauer gravatar image

updated 2012-09-28 02:01:46 -0600

JervisW gravatar image

I think putting code which requires PSSE in its own thread would do the trick. I can't verify the license is not checked out (I'm not sure how to do that), but I'm pretty sure this works:

import sys, os
import threading

class ThreadClass(threading.Thread):
    def __init__(self, caseFilename):
        threading.Thread.__init__(self)
        self.caseFilename = caseFilename

    def run(self):
        # Import psspy and do your code requiring psse here.
        print 'Executing code which DOES require PSSE...'

        PSSE_LOCATION = r"C:\Program Files\PTI\PSSE32\PSSBIN"
        sys.path.append(PSSE_LOCATION)
        os.environ['PATH'] = os.environ['PATH'] + ';' +  PSSE_LOCATION
        import psspy

        psspy.psseinit(100000)
        psspy.case(self.caseFilename)

def main():
    # Do non-PSSE tasks
    print 'Executing code which DOES NOT require PSSE...'
    caseFilename = r'C:\path\to\case\case.sav'


    # Do PSSE tasks
    t = ThreadClass(caseFilename)
    t.start()
    t.join()    # Wait for PSSE process to finish (comment out to run in parallel)

    # Do more non-PSSE tasks
    print 'Test to see if PSSE is avaliable...'
    try:
        print psspy.psseversion()
    except NameError:
        print 'Success!  psspy is not imported, so license should not be checked out'

if __name__ == '__main__':
    main()

(edit - JervisW)

Here is the code that I used to verify that the PSSE license once activated in the thread will continue in the main program.

import threading

def initpsse():
  import psspy
  import os
  import sys

  PSSE_LOCATION = r"C:\Program Files\PTI\PSSE32\PSSBIN"
  sys.path.append(PSSE_LOCATION)
  os.environ['PATH'] = os.environ['PATH'] + ';' +  PSSE_LOCATION
  psspy.psseinit(10000)
  psspy.case('base.sav')

psse_background_thread = threading.Thread(target=initpsse)
psse_background_thread.start()
psse_background_thread.join()

# now to check if the case isn't correctly loaded.
import psspy
psspy.throwPsseExceptions = True

try:
  psspy.case('base.sav')
except psspy.CaseError:
  print "Success! PSSE isn't initialised"
else:
  print "Failure! PSSE is still initialised in the main program"
edit flag offensive delete link more

Comments

I should be able to test this out. I'll let you know how it goes. An interesting experiment.

JervisW gravatar imageJervisW ( 2012-09-19 19:26:26 -0600 )edit

I'm excited to see if it works.

jsexauer gravatar imagejsexauer ( 2012-09-20 06:08:55 -0600 )edit

unfortunately it doesn't work. That `NameError` is due to the fact that you haven't imported `psspy` in the global scope, rather than psspy not being initialised in the main thread. Behind the scenes, psse is globally initialised and closing the thread does not close the license.

JervisW gravatar imageJervisW ( 2012-09-27 20:47:01 -0600 )edit

Perhaps the one way is to run PSSE in a completely separate subprocess and then communicate with the main program somehow through messages. If I get something up and running I'll post a 'how-to'

JervisW gravatar imageJervisW ( 2012-09-27 20:48:16 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2012-09-18 17:50:28 -0600

Seen: 2,132 times

Last updated: Sep 28 '12