Skip to content

Allow the thread limit to be set globally via an environment variable.#242

Merged
zfergus merged 1 commit into
mainfrom
feature/env_num_threads
Jul 13, 2026
Merged

Allow the thread limit to be set globally via an environment variable.#242
zfergus merged 1 commit into
mainfrom
feature/env_num_threads

Conversation

@zfergus

@zfergus zfergus commented Jul 13, 2026

Copy link
Copy Markdown
Member

Description

The thread limit can also be controlled by setting the TBB_NUM_THREADS environment variable before importing ipctk. This is useful in production environments where you want to enforce a global thread limit without modifying every script that uses the IPC Toolkit:

export TBB_NUM_THREADS=1

If set, this takes effect as soon as ipctk is imported (equivalent to calling ipctk.set_num_threads with the given value), and can still be overridden later in the script by calling ipctk.set_num_threads directly. If the variable is unset or invalid, it is ignored and the default thread count is used.

Fixes #241

Type of change

  • Enhancement (non-breaking change which improves existing functionality)

Copilot AI review requested due to automatic review settings July 13, 2026 16:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for setting a global TBB thread limit via the TBB_NUM_THREADS environment variable at ipctk import time, addressing the production use case described in issue #241.

Changes:

  • Reads TBB_NUM_THREADS during module initialization and applies it via set_num_threads.
  • Logs when TBB_NUM_THREADS is present but invalid.
  • Documents the new environment-variable behavior in the misc tutorial.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/src/utils/thread_limiter.cpp Applies an import-time thread limit from TBB_NUM_THREADS.
docs/source/tutorials/misc.rst Documents how to use TBB_NUM_THREADS to control thread count globally.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +47 to +54
const char* env_val = std::getenv("TBB_NUM_THREADS");
if (env_val != nullptr) {
try {
set_num_threads(std::stoi(env_val));
} catch (const std::exception& e) {
logger().error("Invalid value for TBB_NUM_THREADS: {}", env_val);
}
}
Comment on lines +44 to +47
// Allow the thread limit to be set globally via an environment variable
// (e.g., in production) rather than requiring every script to call
// set_num_threads() itself.
const char* env_val = std::getenv("TBB_NUM_THREADS");
@zfergus zfergus merged commit 4d43983 into main Jul 13, 2026
12 checks passed
@zfergus zfergus deleted the feature/env_num_threads branch July 13, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Limit Number of Threads with Environment Variable

2 participants