diff --git a/py-polars/polars/__init__.py b/py-polars/polars/__init__.py index d6c4ae511aa8..f3d52545911c 100644 --- a/py-polars/polars/__init__.py +++ b/py-polars/polars/__init__.py @@ -434,6 +434,10 @@ def __getattr__(name: str) -> Any: # fork() breaks Polars thread pool, so warn users who might be doing this. +# Keep track so that we warn only once +_WARNED = False + + def __install_postfork_hook() -> None: message = """\ Using fork() can cause Polars to deadlock in the child process. @@ -445,14 +449,22 @@ def __install_postfork_hook() -> None: fixed in Python 3.14. Until then, you want to use the "spawn" context instead. See https://docs.pola.rs/user-guide/misc/multiprocessing/ for details. + +If you really know what your doing, you can silence this warning with the warning module +or by setting POLARS_ALLOW_FORKING_THREAD=1. """ + import os - def before_hook() -> None: - import warnings + if os.environ.get("POLARS_ALLOW_FORKING_THREAD") == "1": + return None - warnings.warn(message, RuntimeWarning, stacklevel=2) + def before_hook() -> None: + global _WARNED + if not _WARNED: + import warnings - import os + warnings.warn(message, RuntimeWarning, stacklevel=2) + _WARNED = True if hasattr(os, "register_at_fork"): os.register_at_fork(before=before_hook)