Skip to content

Documentation for proposed API to cover Python thread pool use case#218

Closed
itamarst wants to merge 4 commits into
joblib:masterfrom
itamarst:215-limits-for-thread-pools
Closed

Documentation for proposed API to cover Python thread pool use case#218
itamarst wants to merge 4 commits into
joblib:masterfrom
itamarst:215-limits-for-thread-pools

Conversation

@itamarst

@itamarst itamarst commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@ogrisel some questions:

  1. Does this seem reasonable from a user perspective?
  2. Are there other use cases I'm not thinking of? Writing the docs brought up two, so maybe there are more.

Actual coding depends on #213 and #216.

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Here is some feedback.

Comment thread README.md Outdated
Comment thread README.md Outdated
Comment thread README.md
with ThreadPool(2, initializer=limiter.limit_in_pythread) as pool:
# ... run some BLAS-using code in the thread pool ...
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could warn users that changing the thread pool sizes is expected to have some overhead and should not be done from within an otherwise fast iterating loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not that expensive compared to anything you're likely to run that uses BLAS or OpenMP...

Comment thread README.md Outdated
Comment thread README.md Outdated
decorators are accessible through their `wrap` method.

This should only be used for functions you expect to be called from the main
thread of a process, without the use of any Python thread pools.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this approach also work if you passed such a decorated callable to the POOL.map as an alternative to what is presented in "Switching Back And Forth Between Main Thread and Python Threads"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's called from a thread, then it's not equivalent to the per-thread API, e.g. it won't know to call MKL's thread-specific limiting API and will instead want to use MKL's process-wide limiting API.

Comment thread README.md Outdated
@itamarst

itamarst commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Since it sounded like the API design itself is probably OK, I will probably close this draft PR for now and re-open when there's code to show.

@itamarst itamarst closed this Jul 8, 2026
@ogrisel

ogrisel commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

@itamarst Actually, I thought a bit more about this and I think we don't necessarily need to change the existing code but rather document how to use it correctly in a Python-level threaded app as explained in #215 (comment).

@itamarst itamarst reopened this Jul 9, 2026
@itamarst itamarst closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants