简体   繁体   中英

How do I get my Jython script to synchronously invoke a Java method?

I have a Java library that uses Jython to allow users to add custom scripts to interact with particular parts of the library.

Having Java give data to the scripts is easy, but let's say I want to follow this pattern in on of the Jython scripts:

data = do_stuff()
# Heavy lifting! have the java lib do this
results = java_lib_help_me_out(data)
do_stuff_with_results(results)

Is it possible to do this easily? I realize that I can make the library act as a service and use REST for intercommunication, or I can use callbacks and make the process a bit more roundabout:

data = do_stuff()
#heavy lifting
java_lib_help_me_out(data, callback)
...
def callback(results):
    do_stuff_with_results(results)

Is there a better way to do this?

Just have your java_lib_help_me_out() function return the data as a Java class or array or whatever, like this:

public String[] java_lib_help_me_out()
{
    return new String[] { "one", "two", "three" };
}

Jython should automatically be able to introspect it and let you do all the things you would expect with the results.

If this doesn't work, maybe post more details?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM