Execution timing of @ScriptService

Hi,
I am calling a secondary script from another inside Fiji using the @ScriptService and was wondering if I can influence the execution time of one script versus the other.

It seems that once the secondary script is started, it runs independently from the primary one. Is there a way for the primary script to “wait” until the secondary script is done, and only then proceed?

here is my primary script (jython):

#@ File macro_file
#@ ScriptService scriptService

from ij import IJ
IJ.log("primary scripts starts")

# use the script service to run the macro
scriptService.run(macro_file, True)

IJ.log("primary script is done")

and here is my secondary script (IJ-macro):

print("the secondary macro starts now...");
wait(3000);
print("the secondary macro is done");

the desired output that I would like to get is:

primary scripts starts
the secondary macro starts now…
the secondary macro is done
primary script is done

but instead I get:

primary scripts starts
primary script is done
the secondary macro starts now…
the secondary macro is done

Doe anyone know a way of how to “wrap” the execution of my secondary script into the first one?

Thanks for your help! Kai

Yes. Have a look at the javadoc for the run() method signatures of ScriptService:

Future<ScriptModule> run(File file, boolean process, Object... inputs)

The method returns a Future, a native Java construct that (citing the javadoc) “represents the result of an asynchronous computation.”

You can call its get() method to get a handle on the result of running your script:

scriptModule = scriptService.run(macro_file, True).get()

This will also allow you to access the outputs of your script (provided you define some with #@output myResult) after execution:

output = scriptModule.getOutput("myResult")

Hope that helps.


Edit:
Note that all this is documented in the Fundamentals tutorial notebook, in the Modules/Scripts section:

Any suggestions on how to make this more discoverable and improve the documentation are warmly welcomed, as usual :wink:

1 Like

thanks Jan, this is awesome as per usual :slight_smile:

Were I personal get lost is that I just don’t know all the script parameters and services that exist. If a list of these exists, I would look for it in the imagej wiki
here: https://imagej.net/Script_Parameters

For completes, here my updated scripts:
primary script (python):

#@ File macro_file
#@ ScriptService scriptService

from ij import IJ
IJ.log("primary scripts starts")

scriptModule = scriptService.run(macro_file, True).get()
output = scriptModule.getOutput("my_output")

IJ.log(output)
IJ.log("primary script is done")

secondary script (IJ macro):

#@ output String my_output

print("the secondary macro starts now...");
wait(3000);
my_output = "processing took 3 seconds";
print("the secondary macro is done");
1 Like