WeichenXu123 commented on code in PR #39299: URL: https://github.com/apache/spark/pull/39299#discussion_r1082428873
########## 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 + +from contextlib import closing +import time +import socket +import socketserver +from struct import pack, unpack +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 +_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]: + while self.server.is_active: + packed_number_bytes = self.rfile.read1(4) Review Comment: We should use `rfile.read` instead of `rfile.read1` `rfile.read(nbytes)` runs in blocking mode and ensures nbytes data are read unless it reads until EOF. But `rfile.read1` runs in non-blocking mode and might return any number bytes result. ########## 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 + +from contextlib import closing +import time +import socket +import socketserver +from struct import pack, unpack +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 +_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]: + while self.server.is_active: + packed_number_bytes = self.rfile.read1(4) + if not packed_number_bytes: + time.sleep(_SERVER_POLL_INTERVAL) + continue + number_bytes = unpack("@i", packed_number_bytes)[0] + message = self.rfile.read1(number_bytes) Review Comment: Same with https://github.com/apache/spark/pull/39299/files#r1082428873 -- 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]
