You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public class GraalPythonTests {
// If there is a Cpython library, creating it multiple times will result in an error
static Context CONTEXT = GraalPyResources.contextBuilder(Path.of("/tmp/graalpy"))
.environment(Map.of("TZ", TimeZone.getDefault().getID()))
.build();
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
Future<?> f = executorService.submit(() -> {
CONTEXT.eval("python", """
import time
import numpy
while True:
print(123)
time.sleep(1)
""");
});
try {
f.get(20, TimeUnit.SECONDS);
} catch (Exception e) {
}
}
try {
System.in.read();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Hi, you can use the interrupt method on the context, but that will stop the code on all threads.
Thank you very much for your reply. Currently, I've already switched to an alternative approach by using Java to invoke Python scripts for dynamic code execution (effectively achieving a multi-process setup). I'll consider switching back once GraalPy's support for multiple Context instances becomes mature.
Activity
shuangpai commentedon Dec 9, 2025
package com.company.sometest.code;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.graalvm.python.embedding.GraalPyResources;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.*;
public class GraalPythonTests {
// If there is a Cpython library, creating it multiple times will result in an error
static Context CONTEXT = GraalPyResources.contextBuilder(Path.of("/tmp/graalpy"))
.environment(Map.of("TZ", TimeZone.getDefault().getID()))
.build();
}
[-]代码执行超时问题[/-][+]Code timeout question[/+]msimacek commentedon Dec 10, 2025
Hi, you can use the
interruptmethod on the context, but that will stop the code on all threads.shuangpai commentedon Dec 12, 2025
Thank you very much for your reply. Currently, I've already switched to an alternative approach by using Java to invoke Python scripts for dynamic code execution (effectively achieving a multi-process setup). I'll consider switching back once GraalPy's support for multiple Context instances becomes mature.