From b97a1df3509829e97361fd83817809d803dc5acc Mon Sep 17 00:00:00 2001 From: Koncopd Date: Wed, 8 Jan 2025 15:02:58 +0100 Subject: [PATCH 1/2] Fix signal outside main thread --- lamindb/core/_context.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lamindb/core/_context.py b/lamindb/core/_context.py index 667cbd5f4..acc69ee15 100644 --- a/lamindb/core/_context.py +++ b/lamindb/core/_context.py @@ -4,6 +4,7 @@ import hashlib import signal import sys +import threading import traceback from datetime import datetime, timezone from pathlib import Path @@ -126,8 +127,11 @@ def start(self, run: Run): sys.stdout = LogStreamHandler(self.original_stdout, self.log_file) sys.stderr = LogStreamHandler(self.original_stderr, self.log_file) # handle signals - signal.signal(signal.SIGTERM, self.cleanup) - signal.signal(signal.SIGINT, self.cleanup) + # signal should be used only in the main thread, otherwise + # ValueError: signal only works in main thread of the main interpreter + if threading.current_thread() == threading.main_thread(): + signal.signal(signal.SIGTERM, self.cleanup) + signal.signal(signal.SIGINT, self.cleanup) # handle exceptions sys.excepthook = self.handle_exception From 613ace2e7fca6f23b19fbbe03ff3e3e82ca0bbe4 Mon Sep 17 00:00:00 2001 From: Koncopd Date: Wed, 8 Jan 2025 15:44:14 +0100 Subject: [PATCH 2/2] allow not to log --- lamindb/_finish.py | 2 +- lamindb/core/_context.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lamindb/_finish.py b/lamindb/_finish.py index 4e63a47ff..2198c22c8 100644 --- a/lamindb/_finish.py +++ b/lamindb/_finish.py @@ -42,7 +42,7 @@ def save_run_logs(run: Run, save_run: bool = False) -> None: ) artifact.save(upload=True, print_progress=False) run.report = artifact - if save_run: # defaults to fast because is slow + if save_run: # defaults to false because is slow run.save() diff --git a/lamindb/core/_context.py b/lamindb/core/_context.py index acc69ee15..991082135 100644 --- a/lamindb/core/_context.py +++ b/lamindb/core/_context.py @@ -245,6 +245,7 @@ def track( params: dict | None = None, new_run: bool | None = None, path: str | None = None, + log_to_file: bool | None = None, ) -> None: """Initiate a run with tracked data lineage. @@ -259,10 +260,13 @@ def track( Args: transform: A transform `uid` or record. If `None`, creates a `uid`. params: A dictionary of parameters to track for the run. - new_run: If `False`, loads latest run of transform - (default notebook), if `True`, creates new run (default pipeline). + new_run: If `False`, loads the latest run of transform + (default notebook), if `True`, creates new run (default non-notebook). path: Filepath of notebook or script. Only needed if it can't be automatically detected. + log_to_file: If `True`, logs stdout and stderr to a file and + saves the file within the current run (default non-notebook), + if `False`, does not log the output (default notebook). Examples: @@ -353,7 +357,9 @@ def track( ) self._run = run track_environment(run) - if self.transform.type != "notebook": + if log_to_file is None: + log_to_file = self.transform.type != "notebook" + if log_to_file: self._stream_tracker.start(run) logger.important(self._logging_message_track) if self._logging_message_imports: