Skip to content

Commit

Permalink
fix: Less intrusive forking warnings (#20032)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Nov 27, 2024
1 parent a5c3902 commit 74b9925
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit 74b9925

Please sign in to comment.