robertwb commented on a change in pull request #17035: URL: https://github.com/apache/beam/pull/17035#discussion_r824319140
########## File path: sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/PythonService.java ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.extensions.python; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; +import org.apache.beam.vendor.grpc.v1p43p2.com.google.common.base.Charsets; +import org.apache.beam.vendor.grpc.v1p43p2.com.google.common.collect.ImmutableList; +import org.apache.beam.vendor.grpc.v1p43p2.com.google.common.io.ByteStreams; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Utility to bootstrap and start a Beam Python service. */ +public class PythonService { + private static final Logger LOG = LoggerFactory.getLogger(PythonService.class); + + private final String module; + private final List<String> args; + + public PythonService(String module, List<String> args) { + this.module = module; + this.args = args; + } + + public PythonService(String module, String... args) { + this(module, Arrays.asList(args)); + } + + public AutoCloseable start() throws IOException, InterruptedException { + File bootstrapScript = File.createTempFile("bootstrap_beam_venv", ".py"); + bootstrapScript.deleteOnExit(); + try (FileOutputStream fout = new FileOutputStream(bootstrapScript.getAbsolutePath())) { + ByteStreams.copy(getClass().getResourceAsStream("bootstrap_beam_venv.py"), fout); + } + List<String> bootstrapCommand = ImmutableList.of("python", bootstrapScript.getAbsolutePath()); + LOG.info("Running bootstrap command " + bootstrapCommand); + Process bootstrap = + new ProcessBuilder("python", bootstrapScript.getAbsolutePath()) Review comment: Good question, I actually tried to do it this way originally. The problem is that the `os.execlp` bootstrap from Python 2 to Python 3 doesn't work if it isn't run as a file (as the stdin is already partially consumed). There are still too many systems out there that, despite having Python 3, map `python` to Python 2.x. (An alternative would be to make the full script Python 2 compatible, but that would cause ongoing pain, especially if one can't count on any dependencies like six...) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
