Skip to content

Commit 7e5e84c

Browse files
committed
[GR-40219] Adopt the changes in TruffleContext builder and multiprocessing spawn improvements.
PullRequest: graalpython/2378
2 parents ea8117d + 780a2bd commit 7e5e84c

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,11 +1036,15 @@ public SharedMultiprocessingData getSharedMultiprocessingData() {
10361036

10371037
public long spawnTruffleContext(int fd, int sentinel, int[] fdsToKeep) {
10381038
ChildContextData data = new ChildContextData(isChildContext() ? childContextData.parentCtx : this);
1039-
1040-
Builder builder = data.parentCtx.env.newContextBuilder().config(PythonContext.CHILD_CONTEXT_DATA, data);
1039+
Builder builder = data.parentCtx.env.newInnerContextBuilder().//
1040+
forceSharing(getOption(PythonOptions.ForceSharingForInnerContexts)).//
1041+
inheritAllAccess(true).//
1042+
initializeCreatorContext(true).//
1043+
// TODO always force java posix in spawned: test_multiprocessing_spawn fails
1044+
// with that. Gives "OSError: [Errno 9] Bad file number"
1045+
// option("python.PosixModuleBackend", "java").//
1046+
config(PythonContext.CHILD_CONTEXT_DATA, data);
10411047
Thread thread = data.parentCtx.env.createThread(new ChildContextThread(fd, sentinel, data, builder));
1042-
1043-
// TODO always force java posix in spawned
10441048
long tid = thread.getId();
10451049
getSharedMultiprocessingData().putChildContextThread(tid, thread);
10461050
getSharedMultiprocessingData().putChildContextData(tid, data);
@@ -1066,6 +1070,11 @@ public synchronized List<Integer> getChildContextFDs() {
10661070
}
10671071

10681072
private static class ChildContextThread implements Runnable {
1073+
private static final TruffleLogger MULTIPROCESSING_LOGGER = PythonLanguage.getLogger(ChildContextThread.class);
1074+
private static final Source MULTIPROCESSING_SOURCE = Source.newBuilder(PythonLanguage.ID,
1075+
"from multiprocessing.spawn import spawn_truffleprocess; spawn_truffleprocess(fd, sentinel)",
1076+
"<spawned-child-context>").internal(true).build();
1077+
10691078
private final int fd;
10701079
private final ChildContextData data;
10711080
private final Builder builder;
@@ -1081,28 +1090,24 @@ public ChildContextThread(int fd, int sentinel, ChildContextData data, Builder b
10811090
@Override
10821091
public void run() {
10831092
try {
1084-
LOGGER.fine("starting spawned child context");
1085-
Source source = Source.newBuilder(PythonLanguage.ID,
1086-
"from multiprocessing.spawn import spawn_truffleprocess; spawn_truffleprocess(" + fd + ", " + sentinel + ")",
1087-
"<spawned-child-context>").internal(true).build();
1088-
CallTarget ct;
1093+
MULTIPROCESSING_LOGGER.fine("starting spawned child context");
10891094
TruffleContext ctx = builder.build();
10901095
data.setTruffleContext(ctx);
10911096
Object parent = ctx.enter(null);
1092-
ct = PythonContext.get(null).getEnv().parsePublic(source);
1097+
CallTarget ct = PythonContext.get(null).getEnv().parsePublic(MULTIPROCESSING_SOURCE, "fd", "sentinel");
10931098
try {
10941099
data.running.countDown();
1095-
Object res = ct.call();
1100+
Object res = ct.call(fd, sentinel);
10961101
int exitCode = CastToJavaIntLossyNode.getUncached().execute(res);
10971102
data.setExitCode(exitCode);
10981103
} finally {
10991104
ctx.leave(null, parent);
11001105
if (data.compareAndSetExiting(false, true)) {
11011106
try {
11021107
ctx.close();
1103-
LOGGER.log(Level.FINE, "closed spawned child context");
1108+
MULTIPROCESSING_LOGGER.log(Level.FINE, "closed spawned child context");
11041109
} catch (Throwable t) {
1105-
LOGGER.log(Level.FINE, t, () -> "exception while closing spawned child context");
1110+
MULTIPROCESSING_LOGGER.log(Level.FINE, "exception while closing spawned child context", t);
11061111
}
11071112
}
11081113
data.parentCtx.sharedMultiprocessingData.closePipe(sentinel);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ private PythonOptions() {
286286
@Option(category = OptionCategory.EXPERT, usageSyntax = "true|false", help = "Propagate append operations to lists created as literals back to where they were created, to inform overallocation to avoid having to grow them later.") //
287287
public static final OptionKey<Boolean> OverallocateLiteralLists = new OptionKey<>(true);
288288

289+
@Option(category = OptionCategory.EXPERT, usageSyntax = "true|false", help = "Forces AST sharing for inner contexts.") //
290+
public static final OptionKey<Boolean> ForceSharingForInnerContexts = new OptionKey<>(true);
291+
289292
@EngineOption @Option(category = OptionCategory.USER, usageSyntax = "true|false", help = "Emulate some Jython features that can cause performance degradation") //
290293
public static final OptionKey<Boolean> EmulateJython = new OptionKey<>(false);
291294

graalpython/lib-python/3/multiprocessing/spawn.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def spawn_truffleprocess(fd, parent_sentinel):
144144
self = reduction.pickle.load(bytesIO)
145145
finally:
146146
del process.current_process()._inheriting
147-
exitcode = self._bootstrap(parent_sentinel)
148-
sys.exit(exitcode)
147+
return self._bootstrap(parent_sentinel)
149148
# End Truffle change
150149

151150
def _check_not_importing_main():

0 commit comments

Comments
 (0)