Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4603#discussion_r24712357
  
    --- Diff: 
core/src/main/scala/org/apache/spark/api/python/PythonGatewayServer.scala ---
    @@ -0,0 +1,62 @@
    +/*
    + * 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.spark.api.python
    +
    +import java.io.DataOutputStream
    +import java.net.Socket
    +
    +import py4j.GatewayServer
    +
    +/**
    + * Process that starts a Py4J GatewayServer on an ephemeral port and 
communicates the bound port
    + * back to its caller via a callback port specified by the caller.
    + *
    + * This process is launched (via SparkSubmit) by the PySpark driver (see 
java_gateway.py).
    + */
    +private[spark] object PythonGatewayServer {
    +  def main(args: Array[String]): Unit = {
    +    try {
    +      // Start a GatewayServer on an ephemeral port
    +      val gatewayServer: GatewayServer = new GatewayServer(null, 0)
    +      gatewayServer.start()
    +      val boundPort: Int = gatewayServer.getListeningPort
    +
    +      // Communicate the bound port back to the caller via the 
caller-specified callback port
    +      val callbackHost = sys.env("PYSPARK_DRIVER_CALLBACK_HOST")
    +      val callbackPort = sys.env("PYSPARK_DRIVER_CALLBACK_PORT").toInt
    +      val callbackSocket = new Socket(callbackHost, callbackPort)
    +      val dos = new DataOutputStream(callbackSocket.getOutputStream)
    +      dos.writeInt(boundPort)
    +      dos.close()
    +      callbackSocket.close()
    +
    +      // Exit on EOF or broken pipe to ensure that this process dies when 
the Python driver dies:
    +      while (System.in.read() != -1) {
    +        // Do nothing
    +      }
    +      System.exit(0)
    +    } catch {
    +      case e: Exception =>
    +        try {
    +          throw e
    --- End diff --
    
    Initially, I wasn't sure whether we wanted to add a dependency on Spark's 
`Logging` trait here, since I was worried that having logging initialization 
performed here might cause problems, but on reflection I don't think that's an 
issue, so I'll re-work this to do proper logging.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to