Using Watchers Callbacks

Learn about using watchers callbacks.

We'll cover the following

When a member joins or leaves a group, applications usually want to run an action. There is a mechanism provided by Tooz to help with the named watchers. It works by caching a list of the members and running the specified callback functions each time a member joins or leaves the group.

To use those callbacks, Tooz provides the watch_join_group and watch_leave_ group methods to register functions to call on join or leave events. If you ever need to un-register a callback, unwatch_join_group and unwatch_leave_group provide this functionality.

Once the callback functions are registered, they are only run when the run_watcher method is called. If your application is thread-safe, you can run this method regularly in a different thread. If not, you should call it in any loop your program provides.

Join and leave callbacks using Tooz

The following example provides an example of an application joining a group and checking when a member joins or leaves that group. As soon as this happens and that run_watchers is executed, it prints which member joined or left which group.

Get hands-on with 1200+ tech skills courses.