From 92725b149904f93fff8fdd7ccd1e726938aeffb2 Mon Sep 17 00:00:00 2001 From: Gabriel Georgiev Date: Thu, 28 Sep 2023 11:08:11 +0300 Subject: [PATCH] vdk-jupyter: Enable PYTHONUNBUFFERED to ensure correct log ordering Currently, logs made inside jobs using print statements are not positioned correctly within the vdk_logs file, but rather print logs are appended at the end of the file. This change fixes this by enabling the PYTHONUNBUFFERED env variable to ensure that print statements are not buffered and are written to the file immediately. Testing done: local test Signed-off-by: Gabriel Georgiev --- .../vdk_jupyterlab_extension/vdk_ui.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/projects/vdk-plugins/vdk-jupyter/vdk-jupyterlab-extension/vdk_jupyterlab_extension/vdk_ui.py b/projects/vdk-plugins/vdk-jupyter/vdk-jupyterlab-extension/vdk_jupyterlab_extension/vdk_ui.py index 6628c754f3..dcfd574711 100644 --- a/projects/vdk-plugins/vdk-jupyter/vdk-jupyterlab-extension/vdk_jupyterlab_extension/vdk_ui.py +++ b/projects/vdk-plugins/vdk-jupyter/vdk-jupyterlab-extension/vdk_jupyterlab_extension/vdk_ui.py @@ -67,11 +67,17 @@ def run_job(path, arguments=None): arguments = shlex.quote(arguments) cmd.append("--arguments") cmd.append(f"{arguments}") + + # We add the PYTHONUNBUFFERED env variable to ensure print statements inside user jobs are correctly + # interspersed in between the rest of the job logs instead of being placed at the end + env = os.environ.copy() + env["PYTHONUNBUFFERED"] = "x" + process = subprocess.Popen( cmd, stdout=log_file, stderr=log_file, - env=os.environ.copy(), + env=env, shell=False, ) process.wait()