B4J Question jython instances

Blueforcer

Well-Known Member
Licensed User
Longtime User
Im trying to load several pythonscripts via classes to access each of them individually like plugins.

Im using jPython for this

For each script i declare a new class wich should invoke the corresponding script. Then i add each to a list
But for any reason its always the data from the first script.

Any tip how to solve this? Working the last 6 hours to implement it into my monster project, until i regocnized it doesnt work with more than one script :(

Uploaded a testproject and the jPhyton.java from the lib author @Daestrum


Thanks
 

Attachments

  • jython test.zip
    1.7 KB · Views: 122
  • jPython.java
    1.6 KB · Views: 134
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Ok, I had a play around with this to see the problem.
There is only one scriptengine for python. Irrespective of how many classes you make to initialize it, there is only one.
The library checks if you try to initialize more than once ( invo != null) and just returns.
Thus it never loads the second script.
The call to getName works because there is a function in the first script with that name, but returns the first name 'script1'.

Now, if you remove the return in the invo check, it will load the second script but overwriting the first one, thus returning 'script2'.

hope this helps.

btw, that is an old library
JInvokePython is the better one to use.. see below
I have modified the attached one to ignore the invo check.
Also you have to tell it where the jython libraries are ie the lib folder in the jython directory.
 

Attachments

  • JInvolePython.zip
    5.1 KB · Views: 106
Last edited:
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
Thank you very much!
I will try that as soon as im @home.
But if understood it correctly, i always need to load the corresponding script again for each access?What means that my main program is blocked by the longer loading time (python compiling? Or whatever) with every script switch?

I assume that the jScriptEngine for JS has the same behavior?
 
Last edited:
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
Ok, i found a workaround for that problem without to reload the scripts with every access.

my mainapp searchs for all python files and writes a "gateway" python file. This gateway loads every pythonscript as a module and contains all required functions from all scripts.
So i only need to invoke this gateway script to access all others.
Took me another 4 hours xD

now i need to figure out how to prevent the script from blocking my mainthread. Especially if the script loads data from the internet. Maybe there is also something like "wait for" ?
 
Last edited:
Upvote 0
Top