Skip to content

Commit a9f36e2

Browse files
committed
Add classmethod Reclass.set_thread_count() to configure the Rayon global thread pool
1 parent a28795f commit a9f36e2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub mod types;
1616
use anyhow::{anyhow, Result};
1717
use pyo3::exceptions::PyValueError;
1818
use pyo3::prelude::*;
19+
use pyo3::types::PyType;
20+
use rayon::ThreadPoolBuilder;
1921
use std::collections::HashMap;
2022
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
2123
use walkdir::WalkDir;
@@ -193,6 +195,19 @@ impl Reclass {
193195
Inventory::render(self)
194196
.map_err(|e| PyValueError::new_err(format!("Error while rendering inventory: {e}")))
195197
}
198+
199+
/// Configures the number of threads to use when rendering the full inventory. Calling the
200+
/// method with `count=0` will configure the thread pool to have one thread per logical core of
201+
/// the system.
202+
///
203+
/// Note that this method should only be called once and will print a diagnostic message if
204+
/// called again.
205+
#[classmethod]
206+
pub fn set_thread_count(_cls: &PyType, count: usize) {
207+
if let Err(e) = ThreadPoolBuilder::new().num_threads(count).build_global() {
208+
eprintln!("While initializing global thread pool: {e}");
209+
}
210+
}
196211
}
197212

198213
impl Default for Reclass {

0 commit comments

Comments
 (0)