Allow the thread limit to be set globally via an environment variable.#242
Merged
Conversation
Contributor
There was a problem hiding this comment.
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_THREADSduring module initialization and applies it viaset_num_threads. - Logs when
TBB_NUM_THREADSis 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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The thread limit can also be controlled by setting the
TBB_NUM_THREADSenvironment variable before importingipctk. 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=1If set, this takes effect as soon as
ipctkis imported (equivalent to callingipctk.set_num_threadswith the given value), and can still be overridden later in the script by callingipctk.set_num_threadsdirectly. If the variable is unset or invalid, it is ignored and the default thread count is used.Fixes #241
Type of change