oracle / graalpython

GraalPy – A high-performance embeddable Python 3 runtime for Java
https://www.graalvm.org/python/
Other
1.24k stars 108 forks source link

WITH_THREAD should be kept per Context #190

Closed kariya-mitsuru closed 3 years ago

kariya-mitsuru commented 3 years ago

Please see the sample code below.

============================== sample code ==============================

import org.graalvm.polyglot.Context;

public class MultiContext1 {
    public static void main(String[] args) {
        try (Context ctx1 = Context.newBuilder("python").build();
             Context ctx2 = Context.newBuilder("python")
                .allowExperimentalOptions(true)
                .option("python.WithThread", "true").build()) {

            System.out.println("initialize start");
            ctx1.eval("python", "import _sysconfig");
            ctx2.eval("python", "import _sysconfig");
            System.out.println("initialize end");

            System.out.println("eval start");
            ctx1.eval("python", "print(_sysconfig.get_config_vars())");
            ctx2.eval("python", "print(_sysconfig.get_config_vars())");
            System.out.println("eval end");
        }
    }
}

============================== sample code ==============================

============================== output ==============================

initialize start
initialize end
eval start
{'WITH_THREAD': 1}
{'WITH_THREAD': 1}
eval end

============================== output ==============================

I think WITH_THREAD should be kept per Context, so the output should look like this.

============================== output ==============================

initialize start
initialize end
eval start
{'WITH_THREAD': 0}
{'WITH_THREAD': 1}
eval end

============================== output ==============================

timfel commented 3 years ago

We are introducing proper threading for the next release, so this option will completely go away

timfel commented 3 years ago

Unfortunately, the threading option cannot be per context in any case. We need to be able to share ASTs across contexts in the same engine, but enabling or disabling threads such a deep impact on the ASTs that they become unshareable between those two scenarios.