File tree 3 files changed +19
-2
lines changed
3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ chrono = "0.4.30"
16
16
indexmap = " 2.0.0"
17
17
nom = " 7.1.3"
18
18
pyo3 = { version = " 0.19.2" , features = [" chrono" ] }
19
+ rayon = " 1.7.0"
19
20
serde = { version = " 1.0.188" , features = [" derive" ] }
20
21
serde_json = " 1.0.106"
21
22
serde_yaml = " 0.9.25"
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
2
2
use chrono:: Local ;
3
3
use pyo3:: prelude:: * ;
4
4
use pyo3:: types:: PyDict ;
5
+ use rayon:: prelude:: * ;
5
6
use std:: collections:: HashMap ;
6
7
7
8
use super :: { NodeInfo , Reclass } ;
@@ -28,8 +29,8 @@ impl Inventory {
28
29
// Render all nodes
29
30
let infos: Vec < _ > = r
30
31
. nodes
31
- . keys ( )
32
- . map ( |name| ( name, { r. render_node ( name) } ) )
32
+ . par_iter ( )
33
+ . map ( |( name, _ ) | ( name, { r. render_node ( name) } ) )
33
34
. collect ( ) ;
34
35
35
36
// Generate `Inventory` from the rendered nodes
Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ pub mod types;
16
16
use anyhow:: { anyhow, Result } ;
17
17
use pyo3:: exceptions:: PyValueError ;
18
18
use pyo3:: prelude:: * ;
19
+ use pyo3:: types:: PyType ;
20
+ use rayon:: ThreadPoolBuilder ;
19
21
use std:: collections:: HashMap ;
20
22
use std:: path:: { Component , Path , PathBuf , MAIN_SEPARATOR } ;
21
23
use walkdir:: WalkDir ;
@@ -193,6 +195,19 @@ impl Reclass {
193
195
Inventory :: render ( self )
194
196
. map_err ( |e| PyValueError :: new_err ( format ! ( "Error while rendering inventory: {e}" ) ) )
195
197
}
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
+ }
196
211
}
197
212
198
213
impl Default for Reclass {
You can’t perform that action at this time.
0 commit comments