mirror of
https://github.com/vale981/ray
synced 2025-03-06 10:31:39 -05:00

Current logs API simply returns a str to unblock development and integration. We should add proper log streaming for better UX and external job manager integration. Co-authored-by: Sven Mika <sven@anyscale.io> Co-authored-by: sven1977 <svenmika1977@gmail.com> Co-authored-by: Ed Oakes <ed.nmi.oakes@gmail.com> Co-authored-by: Richard Liaw <rliaw@berkeley.edu> Co-authored-by: Simon Mo <simon.mo@hey.com> Co-authored-by: Avnish Narayan <38871737+avnishn@users.noreply.github.com> Co-authored-by: Jiao Dong <jiaodong@anyscale.com>
31 lines
883 B
Python
31 lines
883 B
Python
import logging
|
|
import os
|
|
from typing import Iterator, Optional
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def file_tail_iterator(path: str) -> Iterator[Optional[str]]:
|
|
"""Yield lines from a file as it's written.
|
|
|
|
Returns lines in batches opportunistically.
|
|
|
|
Returns None until the file exists or if no new line has been written.
|
|
"""
|
|
if not isinstance(path, str):
|
|
raise TypeError(f"path must be a string, got {type(path)}.")
|
|
|
|
while not os.path.exists(path):
|
|
logger.debug(f"Path {path} doesn't exist yet.")
|
|
yield None
|
|
|
|
with open(path, "r") as f:
|
|
lines = ""
|
|
while True:
|
|
curr_line = f.readline()
|
|
# readline() returns empty string when there's no new line.
|
|
if curr_line:
|
|
lines += curr_line
|
|
else:
|
|
yield lines or None
|
|
lines = ""
|