| 254 | #[pyfunction] |
| 255 | #[pyo3(signature = (worker_threads=None, max_blocking_threads=None, thread_stack_size_mb=None))] |
| 256 | fn configure_runtime( |
| 257 | worker_threads: Option<i32>, |
| 258 | max_blocking_threads: Option<i32>, |
| 259 | thread_stack_size_mb: Option<i32>, |
| 260 | ) -> PyResult<()> { |
| 261 | // Validate and convert worker_threads |
| 262 | let validated_worker_threads = if let Some(threads) = worker_threads { |
| 263 | if threads <= 0 { |
| 264 | return Err(PyValueError::new_err(format!( |
| 265 | "worker_threads must be positive, got: {}", |
| 266 | threads |
| 267 | ))); |
| 268 | } |
| 269 | Some(threads as usize) |
| 270 | } else { |
| 271 | None |
| 272 | }; |
| 273 | |
| 274 | // Validate and convert max_blocking_threads |
| 275 | let validated_max_blocking_threads = if let Some(threads) = max_blocking_threads { |
| 276 | if threads <= 0 { |
| 277 | return Err(PyValueError::new_err(format!( |
| 278 | "max_blocking_threads must be positive, got: {}", |
| 279 | threads |
| 280 | ))); |
| 281 | } |
| 282 | Some(threads as usize) |
| 283 | } else { |
| 284 | None |
| 285 | }; |
| 286 | |
| 287 | // Validate and convert thread_stack_size_mb |
| 288 | let validated_thread_stack_size = if let Some(mb) = thread_stack_size_mb { |
| 289 | if mb <= 0 { |
| 290 | return Err(PyValueError::new_err(format!( |
| 291 | "thread_stack_size_mb must be positive, got: {}", |
| 292 | mb |
| 293 | ))); |
| 294 | } |
| 295 | Some((mb as usize) * 1024 * 1024) |
| 296 | } else { |
| 297 | None |
| 298 | }; |
| 299 | |
| 300 | let config = runtime::RuntimeConfig { |
| 301 | worker_threads: validated_worker_threads, |
| 302 | thread_stack_size: validated_thread_stack_size, |
| 303 | enable_blocking_pool: true, |
| 304 | max_blocking_threads: validated_max_blocking_threads, |
| 305 | thread_keep_alive: Some(std::time::Duration::from_secs(10)), |
| 306 | thread_name_prefix: "graphbit-py".to_string(), |
| 307 | }; |
| 308 | |
| 309 | runtime::init_runtime_with_config(config).map_err(errors::to_py_runtime_error)?; |
| 310 | |
| 311 | info!("Runtime configured with custom settings"); |
| 312 | Ok(()) |
| 313 | } |