ninia / jep

Embed Python in Java
Other
1.28k stars 145 forks source link

WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance. #444

Closed onegoldfish closed 1 year ago

onegoldfish commented 1 year ago

Describe the bug I use a Distributed Computing Framework ray in java. And I used jepto call python in the remote method, but I got: WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.

could you please tell me does the jepuses sun.reflect.Reflection.getCallerClass? `

To Reproduce

public class RayDemo {
    public static int square(int x) {
        JepConfig config = new JepConfig();
        config.addIncludePaths("/usr/local/mytest");
        config.addIncludePaths(path);
        SharedInterpreter.setConfig(config);
        SharedInterpreter interp = new SharedInterpreter();
        try{
            interp.eval("from pydemo import *");
            String  res = interp.invoke("square",x).toString();
            return Integer.valueOf(res);
        }finally {

        }
    }

    public static class Counter {

        private int value = 0;

        public void increment() {
            SharedInterpreter interp = new SharedInterpreter();
            try {
                interp.eval("from pydemo import *");//python文件名 invoke
                String  res = interp.invoke("add",this.value).toString();
                this.value= Integer.valueOf(res);
            }finally {

            }

        }

        public int read() {
            return this.value;
        }
    }

    public static void main(String[] args) {
        // Intialize Ray runtime.
        RayRuntimeFactory factory=new RayRuntimeFactory() {
            @Override
            public RayRuntime createRayRuntime() {
                return null;
            }
        };
        Ray.init();
        {
            List<ObjectRef<Integer>> objectRefList = new ArrayList<>();
            // Invoke the `square` method 4 times remotely as Ray tasks.
            // The tasks will run in parallel in the background.
            for (int i = 0; i < 3; i++) {
                objectRefList.add(Ray.task(RayDemo::square, i).remote());
            }
            // Get the actual results of the tasks with `get`.
            System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9]
        }

        {
            List<ActorHandle<Counter>> counters = new ArrayList<>();
            // Create 4 actors from the `Counter` class.
            // They will run in remote worker processes.
            for (int i = 0; i < 6; i++) {
                counters.add(Ray.actor(Counter::new).remote());
            }

            // Invoke the `increment` method on each actor.
            // This will send an actor task to each remote actor.
            for (ActorHandle<Counter> counter : counters) {
                counter.task(Counter::increment).remote();
            }
            // Invoke the `read` method on each actor, and print the results.
            List<ObjectRef<Integer>> objectRefList =
                    counters.stream()
                            .map(counter -> counter.task(Counter::read).remote())
                            .collect(Collectors.toList());
            System.out.println(Ray.get(objectRefList)); // [1, 1, 1, 1]
        }

        {
            try (Interpreter interp = new SharedInterpreter()) {
                interp.eval("import numpy");
                interp.eval("import sys");
                interp.exec("from java.lang import System");//jep执行的python语句中可以引入java包
                interp.exec("sys.s = 'Hello World'");
                interp.exec("print(sys.s)");
                //interp.exec("System.out.println(s)");
                //interp.exec("print(s[1:-1])");
            }
        }

        {
            try (Interpreter interp = new SharedInterpreter()) {
                interp.eval("import numpy");
                interp.eval("import sys");
                interp.exec("from java.lang import System");//jep执行的python语句中可以引入java包
                //interp.exec("s = 'Hello World'");
                interp.exec("print(sys.s)");
            }
        }

    }

pydemo .py

def add(a):
    return a + 1

def square(x):
    return x * x

Environment (please complete the following information): jdk11 ubuntu20.0.4 maven ray2.0.0 python3.9.12 jep4.1.0

Additional context Add any other context about the problem here.

bsteffensmeier commented 1 year ago

Jep does not use sun.reflect.Reflection.getCallerClass