Skip to content

Opt-in to using thread pools for all parallelism (or almost all) #34071

Open

Description

@itamarst

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

Describe the workflow you want to enable

User perspective: By default, most parallelism in sklearn uses process pools. On free-threaded Python, however, threading may well have lower overhead: certainly less memory, but quite possibly it will be faster as well.

There are exceptions. E.g. as discussed in #34070, parallel algorithms that can only use a limited number of cores may be better run as process pools.

The user experience should ideally be "use a thread pool if it's known to be faster." But it's difficult to be sure in the short term whether that's true without further work. And maybe it's situational.

Until sklearn reaches the point where it's automatically choosing the fastest option, in the short term a user would want to be able try as easily as possible to see if threads everywhere speed things up.

Maintainer perspective: When testing for thread-safety, or benchmarking for speed, it's useful to be able to have all parallel algorithms use thread pools by default, whether in a CI job or in local testing.

Describe your proposed solution

Add an environment variable SKLEARN_DEFAULT_PARALLEL_BACKEND that can be set to the relevant joblib backends (loky/threading/serial).

Describe alternatives you've considered, if relevant

  • An env variable could be added to joblib, and then it would be JOBLIB_DEFAULT_PARALLEL_BACKEND.
  • This could be a Python API, but that means e.g. pytest needs to grow more options so it can call the API, and puts (a bit) more burden on users who want to experiment.
  • Free-threaded CPython builds could opt-in to thread pools by default. In the long term this may be a better option, in the short term I'd worry about correctness; seems better to change defaults here incrementally.

Additional context

This breaks out one tiny aspect of #34057, where this was done as part of a flag to pytest, and I received feedback disliking such flags. And also this is more generally useful.

Activity

  1. added
    Needs Decision
    free-threading
    and removed
    Needs Triage
    on May 24, 2026
  2. github-actions commented on May 24, 2026

    @github-actions

    Thanks for the work you've done so far. The goal of this comment is to set expectations.

    Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.

    Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.

  3. david-cortes-intel commented on May 25, 2026

    @david-cortes-intel
    Contributor

    Perhaps a stupid question but: wouldn't you already get this by configuring the default backend in joblib before running something with scikit-learn?
    https://joblib.readthedocs.io/en/stable/generated/joblib.parallel_config.html

  4. ogrisel commented on May 26, 2026

    @ogrisel
    Member

    +1 for leveraging joblib's parallel_config in the pytest fixture rather than implementing this logic in a custom wrapper. However, joblib only allows to set the backend programmatically, so we need the pytest fixture to either look for a CLI flag or a custom environment variable to change the default backend to "threading" when running the tests.

    EDIT: I hadn't realized that the scope of this issue was for end-users rather than giving maintainers more options to run the tests.

    For end-users, @david-cortes-intel's suggestion to use joblib.parallel_config is the way to go.

  5. ogrisel commented on May 26, 2026

    @ogrisel
    Member

    Adding an env var to change the default backend in joblib is also reasonable but would rather not do that in scikit-learn itself.

  6. ogrisel commented on May 26, 2026

    @ogrisel
    Member

    Free-threaded CPython builds could opt-in to thread pools by default. In the long term this may be a better option, in the short term I'd worry about correctness; seems better to change defaults here incrementally.

    I agree, we need more testing (thread-safety/correctness & performance) before considering automatically switching to threading as the default backend in joblib and scikit-learn on free-threading Python.

  7. itamarst commented on May 28, 2026

    @itamarst
    ContributorAuthor

    So, limiting the scope of this issue: as I start testing various thread-safety things, and wanting to add some simple assertions, it's useful to have a pytest flag that enables thread pools everywhere for all Parallel operations. And then given that flag, to have a corresponding CI job that runs the tests using that flag.

    I know someone indicated a dislike of having extra pytest flags to remember, but this is a behavior change, it's pretty much something you want to turn on and off in different CI jobs, you don't want this always on (if it's always on process pools won't get tested).

    An alternative would be having pytest run all tests twice, once with thread pool and once with process pool but it's already pretty slow...

    Is this smaller scope acceptable?

  8. itamarst commented on May 28, 2026

    @itamarst
    ContributorAuthor

    It may be that the proposed combo pytest flag in #34057 will be a suitable implementation of the above limited scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs DecisionNew Featurefree-threading

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions