Leader Election

Leader Election
  Tooz中每个组都可以选择自己的领袖。但是每个组在一个时间里只能有一个领袖。
  下面的例子展示了基本的选举。

import time
import uuid

import six

from tooz import coordination

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

# Create a group
group = six.binary_type(six.text_type(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()

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


def when_i_am_elected_leader(event):
    # event is a LeaderElected event
    print(event.group_id, event.member_id)


# Propose to be a leader for the group
coordinator.watch_elected_as_leader(group, when_i_am_elected_leader)

start = time.time()
while time.time() - start < ALIVE_TIME:
    coordinator.heartbeat()
    coordinator.run_watchers()
    time.sleep(0.1)

coordinator.stop()

  程序输出:

('8a9eb390-242a-4ec0-bd74-dbec9f8f6aea', 'host-1')


参考资料:
  Leader Election