Group Membership

Basic operations

One of the feature provided by the coordinator is the ability to handle group membership. Once a group is created, any coordinator can join the group and become a member of it. Any coordinator can be notified when a members joins or leaves the group.

import uuid

from tooz import coordination

coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()

# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()

# Join a group
request = coordinator.join_group(group)
request.get()

coordinator.stop()

Note that all the operation are asynchronous. That means you cannot be sure that your group has been created or joined before you call the tooz.coordination.CoordAsyncResult.get() method.

You can also leave a group using the tooz.coordination.CoordinationDriver.leave_group() method. The list of all available groups is retrievable via the tooz.coordination.CoordinationDriver.get_groups() method.

Watching Group Changes

It’s possible to watch and get notified when the member list of a group changes. That’s useful to run callback functions whenever something happens in that group.

import uuid

from tooz import coordination

coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()

# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()


def group_joined(event):
    # Event is an instance of tooz.coordination.MemberJoinedGroup
    print(event.group_id, event.member_id)


coordinator.watch_join_group(group, group_joined)
coordinator.stop()

Using tooz.coordination.CoordinationDriver.watch_join_group() and tooz.coordination.CoordinationDriver.watch_leave_group() your application can be notified each time a member join or leave a group. To stop watching an event, the two methods tooz.coordination.CoordinationDriver.unwatch_join_group() and tooz.coordination.CoordinationDriver.unwatch_leave_group() allow to unregister a particular callback.