Tweak of :class:`joblib.Parallel` that propagates the scikit-learn configuration. This subclass of :class:`joblib.Parallel` ensures that the active configuration (thread-local) of scikit-learn is propagated to the parallel workers for the duration of the execution of the parallel tasks.
| 39 | |
| 40 | |
| 41 | class Parallel(joblib.Parallel): |
| 42 | """Tweak of :class:`joblib.Parallel` that propagates the scikit-learn configuration. |
| 43 | |
| 44 | This subclass of :class:`joblib.Parallel` ensures that the active configuration |
| 45 | (thread-local) of scikit-learn is propagated to the parallel workers for the |
| 46 | duration of the execution of the parallel tasks. |
| 47 | |
| 48 | The API does not change and you can refer to :class:`joblib.Parallel` |
| 49 | documentation for more details. |
| 50 | |
| 51 | .. versionadded:: 1.3 |
| 52 | """ |
| 53 | |
| 54 | def __call__(self, iterable): |
| 55 | """Dispatch the tasks and return the results. |
| 56 | |
| 57 | Parameters |
| 58 | ---------- |
| 59 | iterable : iterable |
| 60 | Iterable containing tuples of (delayed_function, args, kwargs) that should |
| 61 | be consumed. |
| 62 | |
| 63 | Returns |
| 64 | ------- |
| 65 | results : list |
| 66 | List of results of the tasks. |
| 67 | """ |
| 68 | # Capture the thread-local scikit-learn configuration at the time |
| 69 | # Parallel.__call__ is issued since the tasks can be dispatched |
| 70 | # in a different thread depending on the backend and on the value of |
| 71 | # pre_dispatch and n_jobs. |
| 72 | config = get_config() |
| 73 | # In free-threading Python >= 3.14, warnings filters are managed through a |
| 74 | # ContextVar and warnings.filters is not modified inside a |
| 75 | # warnings.catch_warnings context. You need to use warnings._get_filters(). |
| 76 | # For more details, see |
| 77 | # https://docs.python.org/3.14/whatsnew/3.14.html#concurrent-safe-warnings-control |
| 78 | filters_func = getattr(warnings, "_get_filters", None) |
| 79 | warning_filters = ( |
| 80 | filters_func() if filters_func is not None else warnings.filters |
| 81 | ) |
| 82 | |
| 83 | iterable_with_config_and_warning_filters = ( |
| 84 | ( |
| 85 | _with_config_and_warning_filters(delayed_func, config, warning_filters), |
| 86 | args, |
| 87 | kwargs, |
| 88 | ) |
| 89 | for delayed_func, args, kwargs in iterable |
| 90 | ) |
| 91 | return super().__call__(iterable_with_config_and_warning_filters) |
| 92 | |
| 93 | |
| 94 | # remove when https://github.com/joblib/joblib/issues/1071 is fixed |
no outgoing calls
searching dependent graphs…