Skip to content

Code timeout question #571

Open
Open

Description

@shuangpai

What else can I do to terminate the timeout code when I use the single instance CONTEXT that cannot be closed

Activity

  1. shuangpai commented on Dec 9, 2025

    @shuangpai
    Author

    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();

    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);
        }
    
    }
    

    }

  2. changed the title [-]代码执行超时问题[/-] [+]Code timeout question[/+] on Dec 9, 2025
  3. msimacek commented on Dec 10, 2025

    @msimacek
    Contributor

    Hi, you can use the interrupt method on the context, but that will stop the code on all threads.

  4. shuangpai commented on Dec 12, 2025

    @shuangpai
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions