Does GraalVM polyglot api support "interuptions" #9317
-
Aka: "can I set execution timeouts before running polyglot code and resume it later" |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You can interrupt any execution of polyglot code with Here is a quick example of how to interrupt JavaScript guest execution: public static void main(String[] args) throws ReflectiveOperationException {
var timer = new java.util.Timer(true);
try (Context c = Context.create()) {
timer.schedule(new TimerTask() {
@Override
public void run() {
// maximum interruption time
try {
c.interrupt(Duration.ofMinutes(5));
} catch (TimeoutException e) {
// failed to interrupt in time.
// its possible that the guest application denies an interrupt.
// only thing you can do is cancel now with:
// c.close(true) which cannot be denied by the guest application
}
}
}, 500);
try {
c.eval("js", "while(true);");
} catch (PolyglotException e) {
if (e.isInterrupted()) {
e.printStackTrace();
// resume evaluation - depends on the use-case what to do here / whether possible
}
}
} finally {
timer.cancel();
}
} If your goal is to implement a workflow engine with continuations, then some language implementations like Espresso have language-specific support for continuations. See here https://github.com/oracle/graal/blob/master/espresso/docs/continuations.md |
Beta Was this translation helpful? Give feedback.
-
so alternitively is there a way to in the polyglot'd code (wasm,llvm,python,js,etc...) to yield saying "I have exited you can run other stuff now" |
Beta Was this translation helpful? Give feedback.
-
Sure, but I think you would need an event loop structure to manage your tasks, and a method to call out to the host to tell it that a context can be shelved while other work is performed. It could in theory be resumed by a guest method which is plucked from the context back to the host. |
Beta Was this translation helpful? Give feedback.
You can interrupt any execution of polyglot code with
Context.interrupt()
from any thread (for example a timer). But that will trigger an exception in the polyglot code, so resuming at the same location is not easily possible. You would need to take care of resuming yourself.Here is a quick example of how to interrupt JavaScript guest execution: