Skip to content

Commit 6e19770

Browse files
smendez-hrtclaude
andcommitted
Respect NUMEXPR_MAX_THREADS when setting numexpr thread count
Avoid calling numexpr.set_num_threads() with a value exceeding NUMEXPR_MAX_THREADS to prevent numexpr from printing a warning to stderr during blosc2 import. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5d7dc9a commit 6e19770

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/blosc2/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# ruff: noqa: E402 - Module level import not at top of file
1010
# ruff: noqa: F401 - `var` imported but unused
1111

12+
import contextlib
13+
import os
1214
import platform
1315
from enum import Enum
1416

@@ -402,7 +404,14 @@ def _raise(exc):
402404
nthreads -= nthreads // 8
403405
if not IS_WASM:
404406
# WASM does not support threading
405-
numexpr.set_num_threads(nthreads)
407+
# Only call set_num_threads if within NUMEXPR_MAX_THREADS limit to avoid warning
408+
numexpr_max_env = os.environ.get("NUMEXPR_MAX_THREADS")
409+
numexpr_max: int | None = None
410+
if numexpr_max_env is not None:
411+
with contextlib.suppress(ValueError):
412+
numexpr_max = int(numexpr_max_env)
413+
if numexpr_max is None or nthreads <= numexpr_max:
414+
numexpr.set_num_threads(nthreads)
406415

407416
# This import must be before ndarray and schunk
408417
from .storage import ( # noqa: I001

tests/test_numexpr_threads.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#######################################################################
2+
# Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org>
3+
# All rights reserved.
4+
#
5+
# SPDX-License-Identifier: BSD-3-Clause
6+
#######################################################################
7+
8+
import os
9+
import subprocess
10+
import sys
11+
12+
13+
def test_numexpr_max_threads_no_warning():
14+
"""Test that importing blosc2 with NUMEXPR_MAX_THREADS set does not produce a warning.
15+
16+
When NUMEXPR_MAX_THREADS is set to a value lower than the number of threads
17+
blosc2 would use, we should NOT call numexpr.set_num_threads() to avoid
18+
the numexpr warning being printed to stderr.
19+
"""
20+
# Inherit the current environment but set NUMEXPR_MAX_THREADS to a low value
21+
env = os.environ.copy()
22+
env["NUMEXPR_MAX_THREADS"] = "1"
23+
24+
result = subprocess.run(
25+
[sys.executable, "-c", "import blosc2; print(blosc2.__version__)"],
26+
capture_output=True,
27+
text=True,
28+
env=env,
29+
check=True,
30+
)
31+
32+
# Check that no warning about NUMEXPR_MAX_THREADS was printed
33+
assert "NUMEXPR_MAX_THREADS" not in result.stderr, (
34+
f"Unexpected numexpr warning in stderr: {result.stderr}"
35+
)
36+
assert "nthreads cannot be larger" not in result.stderr, (
37+
f"Unexpected numexpr warning in stderr: {result.stderr}"
38+
)

0 commit comments

Comments
 (0)