Suppose I have a list of Python scripts, 'script1.py', 'script2.py', 'script3.py', etc. Using Jython (which is embedded into my Java application), my goal is to run each script when the Java program is started. Additionally, each script should run within its own environment (I.E. have its own global scope). Scripts may or may not implement multithreading, event logic, etc., so I cannot guarantee that a script terminates prior to running another script.
My understanding is that I should use Jython's PythonInterpreter, but all examples I can find on the usage of PythonInterpreter do not cover my specific use case. My understanding is that all code that is run through a single PythonInterpreter shares the same global scope.
My current methodology is to create a new PythonInterpreter for each script, to ensure each script is isolated. Additionally, I defer handling of the PySystemState (Jython's implementation of the sys module) to Jython, and it seems that Jython creates a "default" PySystemState that is being used for all of the PythonInterpreters I create. Is this sufficient, or should I also be initializing a new PySystemState for each PythonIntepreter?
I am also unsure of the proper usage of the PythonInterpreter#close() method in my specific case, since this method appears to close the associated PySystemState as well. If Jython assigns a single PySystemState to each separate PythonInterpreter in my program, this could be problematic if I close the PythonInterpreter for one script (if it finished execution) while other scripts are still running.
My approach works as expected, so long as I do not close the PythonInterpreter once a script finishes execution; however, I am unsure if my approach is the correct/most efficient way.