WeichenXu123 commented on code in PR #39299: URL: https://github.com/apache/spark/pull/39299#discussion_r1080808104
########## python/pyspark/ml/torch/log_communication.py: ########## @@ -0,0 +1,201 @@ +# +# 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. +# +# type: ignore + +import time +import socket +import socketserver +import sys +import threading +import traceback +from typing import Optional, Generator +import warnings +from pyspark.context import SparkContext + +# Use b'\x00' as separator instead of b'\n', because the bytes are encoded in utf-8 +_SEP_CHAR = b"\x00" +_SERVER_POLL_INTERVAL = 0.1 +_TRUNCATE_MSG_LEN = 4000 + + +def get_driver_host(sc: SparkContext) -> Optional[str]: + return sc.getConf().get("spark.driver.host") + + +_log_print_lock = threading.Lock() # pylint: disable=invalid-name + + +def _get_log_print_lock() -> threading.Lock: + return _log_print_lock + + +class WriteLogToStdout(socketserver.StreamRequestHandler): + def _read_bline(self) -> Generator[bytes, None, None]: + remaining_data = b"" + while self.server.is_active: + new_data = self.rfile.read1(4096) + if not new_data: + time.sleep(_SERVER_POLL_INTERVAL) + continue + blines = new_data.split(_SEP_CHAR) + blines[0] = remaining_data + blines[0] + for i in range(len(blines) - 1): + yield blines[i] + # The last line in blines is a half line. + remaining_data = blines[-1] # pylint: disable=attribute-defined-outside-init Review Comment: * serialize /deser int via `struct.pack / struct.unpack` * send serialized bytes via socket -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
