You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by ro...@apache.org on 2022/04/14 22:34:02 UTC

[beam] branch master updated: [BEAM-17035] Call python3 directly when it is available. (#17366)

This is an automated email from the ASF dual-hosted git repository.

robertwb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new cef866e0aa3 [BEAM-17035] Call python3 directly when it is available. (#17366)
cef866e0aa3 is described below

commit cef866e0aa308f4115d96679e02a103149e36cc4
Author: Robert Bradshaw <ro...@gmail.com>
AuthorDate: Thu Apr 14 15:33:55 2022 -0700

    [BEAM-17035] Call python3 directly when it is available. (#17366)
---
 .../beam/sdk/extensions/python/PythonService.java   | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/PythonService.java b/sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/PythonService.java
index e4a59939d91..346227df1a9 100644
--- a/sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/PythonService.java
+++ b/sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/PythonService.java
@@ -57,12 +57,11 @@ public class PythonService {
     try (FileOutputStream fout = new FileOutputStream(bootstrapScript.getAbsolutePath())) {
       ByteStreams.copy(getClass().getResourceAsStream("bootstrap_beam_venv.py"), fout);
     }
-    List<String> bootstrapCommand = ImmutableList.of("python", bootstrapScript.getAbsolutePath());
+    List<String> bootstrapCommand =
+        ImmutableList.of(whichPython(), bootstrapScript.getAbsolutePath());
     LOG.info("Running bootstrap command " + bootstrapCommand);
     Process bootstrap =
-        new ProcessBuilder("python", bootstrapScript.getAbsolutePath())
-            .redirectError(ProcessBuilder.Redirect.INHERIT)
-            .start();
+        new ProcessBuilder(bootstrapCommand).redirectError(ProcessBuilder.Redirect.INHERIT).start();
     bootstrap.getOutputStream().close();
     BufferedReader reader =
         new BufferedReader(new InputStreamReader(bootstrap.getInputStream(), Charsets.UTF_8));
@@ -96,6 +95,18 @@ public class PythonService {
     return p::destroy;
   }
 
+  private String whichPython() {
+    for (String executable : ImmutableList.of("python3", "python")) {
+      try {
+        new ProcessBuilder(executable, "--version").start().waitFor();
+        return executable;
+      } catch (IOException | InterruptedException exn) {
+        // Ignore.
+      }
+    }
+    throw new RuntimeException("Unable to find a suitable Python executable.");
+  }
+
   public static int findAvailablePort() throws IOException {
     ServerSocket s = new ServerSocket(0);
     try {
@@ -127,6 +138,6 @@ public class PythonService {
     throw new TimeoutException(
         "Timeout waiting for Python service startup after "
             + (System.currentTimeMillis() - start)
-            + " seconds.");
+            + " milliseconds.");
   }
 }