Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Params support for unwatching #4360

Closed
analog-cbarber opened this issue Apr 3, 2020 · 5 comments
Closed

Params support for unwatching #4360

analog-cbarber opened this issue Apr 3, 2020 · 5 comments
Labels
type: enhancement Minor feature or improvement to an existing feature

Comments

@analog-cbarber
Copy link

The Params stream class will watch the parameters by default:

        if watch:
            # Subscribe to parameters
            keyfn = lambda x: id(x.owner)
            for _, group in groupby(sorted(parameters, key=keyfn)):
                group = list(group)
                group[0].owner.param.watch(self._watcher, [p.name for p in group])

but it does not save the created Watcher objects returned by the watch method,
so there is no easy way to unwatch them later. So if you are done with the stream but
not the underlying parameters the stream will be kept alive and will continue to get events.

You can work around this by constructing with watch=False and duplicating
the code above, but it would be much nicer if this ability was built into Params
itself.

@philippjfr philippjfr added the type: enhancement Minor feature or improvement to an existing feature label Apr 3, 2020
@petros1999
Copy link
Contributor

Hi there. Μe and my collaborator, would like to work with that issue. As novices to programming we are, we would appreciate any suggestions or guidelines conserning the building of the upper ability into Params, in order to have a more favorable start of our work. Thanks in advance

@analog-cbarber
Copy link
Author

I just made a subclass to work around this:

class ParamStream(holoviews.streams.Params):
    """Extends :class:`holoviews.streams.Params` class.

    Specifically provides the ability to unregister parameter watching
    through the :meth:`unwatch` method.
    """

    def __init__(self, parameterized=None, parameters=None, watch=True, watch_only=False, **params):
        super().__init__(
            parameterized = parameterized,
            parameters = parameters,
            watch = False,
            watch_only = False,
            **params
        )

        # Super class does not keep track of watchers, so we have to duplicate code
        # here. See https://github.com/holoviz/holoviews/issues/4360.
        self._watchers = [] # type: List[Watcher]
        if watch:
            # Subscribe to parameters
            keyfn = lambda x: id(x.owner)
            for _, group_iter in groupby(sorted(self.parameters, key=keyfn)):
                group = list(group_iter)
                watcher = group[0].owner.param.watch(self._watcher, [p.name for p in group])
                self._watchers.append(watcher)

    def unwatch(self):
        """Stop watching parameters."""
        for watcher in self._watchers:
            watcher.inst.param.unwatch(watcher)
        self._watchers.clear()

Feel free to copy.

@philippjfr
Copy link
Member

We'd very much appreciate a PR adding the unwatch support. Let me know if you need any help contributing. Sadly we don't have a developer guide yet, but you could roughly follow this one: https://panel.holoviz.org/developer_guide/index.html and then replace the develop_install step with this:

doit develop_install -c pyviz/label/dev  -c bokeh -c conda-forge -o unit_tests

@philippjfr
Copy link
Member

philippjfr commented May 11, 2020

Thanks for contributing @alexbraditsas!

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants