Skip to content

[Callbacks] EFF Single manager for all callbacks#33408

Merged
lesteve merged 1 commit intoscikit-learn:callbacksfrom
jeremiedbb:callback-global-manager
Feb 27, 2026
Merged

[Callbacks] EFF Single manager for all callbacks#33408
lesteve merged 1 commit intoscikit-learn:callbacksfrom
jeremiedbb:callback-global-manager

Conversation

@jeremiedbb
Copy link
Copy Markdown
Member

Closes #33325

Create a sklearn global dedicated manager for all callbacks.

ping @FrancoisPgm @StefanieSenger @lesteve @ogrisel

@jeremiedbb jeremiedbb added Quick Review For PRs that are quick to review Callbacks No Changelog Needed labels Feb 25, 2026

class _CallbackManagerState:
manager = None
lock = Lock()
Copy link
Copy Markdown
Member

@lesteve lesteve Feb 26, 2026

Choose a reason for hiding this comment

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

Just curious, do you have a use case in mind where get_callback_manager would be used in a multi-threaded context?

TIL about the double-checked locking pattern ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it when Parallel(n_jobs=..., prefer="threads")?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the last meeting we decided to allow the following:

cb = MetricMonitor("accuracy")
LogisticRegression(C=1).set_callbacks(cb).fit(X,y)
LogisticRegression(C=10).set_callbacks(cb).fit(X,y)
cb.plot()

Then nothing prevents the user to the same but in 2 different threads, in a joblib // loop for instance as Stefanie mentioned

@jeremiedbb jeremiedbb moved this to In progress in Labs Feb 26, 2026
@jeremiedbb jeremiedbb added this to Labs Feb 26, 2026
Copy link
Copy Markdown
Member

@StefanieSenger StefanieSenger left a comment

Choose a reason for hiding this comment

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

Thank you @jeremiedbb!

I have checked how this works out and now I can only see one single subprocess even after multiple runs of PragressBar.

I have posted a few questions in the comments.

from sklearn.callback._callback_context import CallbackContext


class _CallbackManagerState:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it the plan that all callback types would share the same Manager() instance?
Do early stopping, logging, ... callbacks surely also need one?

Should we expose _CallbackManagerState publicly to make implementing custom callbacks easier for
third party devs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it the plan that all callback types would share the same Manager() instance?

Yes, almost all callbacks that we thought about need some kind of shared data structure (queue, list, dict, ...)
We only need one manager process to manage all these shared objects.

Should we expose _CallbackManagerState publicly to make implementing custom callbacks easier for
third party devs?

not this class, but the get_callback_manager function yes. I think we should keep it private for the incoming release and consider making it public for the next one when we add documentation for implementing custom callbacks

Copy link
Copy Markdown
Member

@StefanieSenger StefanieSenger left a comment

Choose a reason for hiding this comment

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

As far as I can tell, this looks good to me.
Thank you @jeremiedbb!

@lesteve lesteve merged commit 54a93fc into scikit-learn:callbacks Feb 27, 2026
39 of 42 checks passed
@github-project-automation github-project-automation bot moved this from In progress to Done in Labs Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Callbacks No Changelog Needed Quick Review For PRs that are quick to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants