Zookeeper in-depth understanding (iii) Kazoo interface

Source: Internet
Author: User

Zookeeper's development interface was mainly Java and C, with the Python project more and more using zookeeper as a distributed cluster implementation, Python Zookeeper interface also appeared a lot, Now the mainstream pure Python zookeeper interface is kazoo. So how to use kazoo to develop a python-based distributed program is a must.

1. Installing Kazoo

Yum Install Python-pip

Pip Install Kazoo

Some python dependent packages are not installed during the installation process and can be installed.

2. Run Kazoo Basic Example kazoo_basic.py

Import time

From kazoo.client import kazooclient

From kazoo.client import kazoostate

def main ():

Zk=kazooclient (hosts= ' 127.0.0.1:2182 ')

Zk.start ()

@zk. Add_listener

def my_listener (state):

if state = = Kazoostate.lost:

Print ("LOST")

elif state = = kazoostate.suspended:

Print ("SUSPENDED")

Else

Print ("Connected")

#Creating Nodes

# Ensure a path, create if necessary

Zk.ensure_path ("/my/favorite")

# Create a node with data

Zk.create ("/my/favorite/node", B "")

Zk.create ("/my/favorite/node/a", B "a")

#Reading Data

# Determine if a node exists

If Zk.exists ("/my/favorite"):

Print ("/my/favorite is existed")

@zk. Childrenwatch ("/my/favorite/node")

def watch_children (children):

Print ("Children is now:%s"% children)

# Above function called immediately, and from then on

@zk. Datawatch ("/my/favorite/node")

def watch_node (data, stat):

Print ("Version:%s, data:%s"% (Stat.version, Data.decode ("Utf-8")))

# Print The version of a node and its data

Data, stat = Zk.get ("/my/favorite/node")

Print ("Version:%s, data:%s"% (Stat.version, Data.decode ("Utf-8")))

# List The Children

Children = Zk.get_children ("/my/favorite/node")

Print ("There is%s children with names%s"% (len (children), children))

#Updating Data

Zk.set ("/my/favorite", B "some data")

#Deleting Nodes

Zk.delete ("/my/favorite/node/a")

#Transactions

Transaction = Zk.transaction ()

Transaction.check ('/my/favorite/node ', version=-1)

Transaction.create ('/my/favorite/node/b ', B "B")

Results = Transaction.commit ()

Print ("Transaction results is%s"% results)

Zk.delete ("/my/favorite/node/b")

Zk.delete ("/my", Recursive=true)

Time.sleep (2)

Zk.stop ()

if __name__ = = "__main__":

Try

Main ()

Except Exception, ex:

Print "ocurred Exception:%s"% str (ex)

Quit ()

Operation Result:

Children is now: [u ' a ']

version:0, Data:

version:0, Data:

There is 1 children with names [u ' a ']

Children is now: []

Transaction results is [True, U '/my/favorite/node/b ']

Children is now: [u ' B ']

Children is now: []

No handlers could be found for logger "kazoo.recipe.watchers"

LOST

The above program runs the basic Kazoo interface command, including the creation of delete plus watcher operations, through debugging and Contrast zookeeper service node Znode directory structure changes, you can understand the specific operation results.

3. Run the Distributed lock program implemented through Kazoo kazoo_lock.py

Import logging, OS, time

From kazoo.client import kazooclient

From kazoo.client import kazoostate

From Kazoo.recipe.lock Import lock

Class Zookeeperlock ():

def __init__ (self, hosts, id_str, Lock_name, Logger=none, timeout=1):

Self.hosts = Hosts

Self.id_str = Id_str

Self.zk_client = None

Self.timeout = Timeout

Self.logger = Logger

Self.name = Lock_name

Self.lock_handle = None

Self.create_lock ()

def create_lock (self):

Try

Self.zk_client = Kazooclient (hosts=self.hosts, Logger=self.logger, Timeout=self.timeout)

Self.zk_client.start (Timeout=self.timeout)

Except Exception, ex:

Self.init_ret = False

Self.err_str = "Create kazooclient failed! Exception:%s "% str (ex)

Logging.error (SELF.ERR_STR)

Return

Try

Lock_path = Os.path.join ("/", "locks", Self.name)

Self.lock_handle = Lock (self.zk_client, Lock_path)

Except Exception, ex:

Self.init_ret = False

SELF.ERR_STR = "Create lock failed! Exception:%s "% str (ex)

Logging.error (SELF.ERR_STR)

Return

def destroy_lock (self):

#self. Release ()

If self.zk_client! = None:

Self.zk_client.stop ()

Self.zk_client = None

def acquire (self, blocking=true, Timeout=none):

if Self.lock_handle = = None:

Return None

Try

Return Self.lock_handle.acquire (blocking=blocking, Timeout=timeout)

Except Exception, ex:

SELF.ERR_STR = "Acquire lock failed! Exception:%s "% str (ex)

Logging.error (SELF.ERR_STR)

Return None

def release (self):

if Self.lock_handle = = None:

Return None

Return Self.lock_handle.release ()

def __del__ (self):

Self.destroy_lock ()

def main ():

Logger = Logging.getlogger ()

Logger.setlevel (Logging.info)

SH = logging. Streamhandler ()

Formatter = logging. Formatter ('% (asctime) s-% (module) s:% (filename) s-l% (Lineno) d-% (levelname) s:% (message) s ')

Sh.setformatter (Formatter)

Logger.addhandler (SH)

zookeeper_hosts = "127.0.0.1:2182"

Lock_name = "Test"

Lock = Zookeeperlock (Zookeeper_hosts, "myID is 1", Lock_name, Logger=logger)

ret = Lock.acquire ()

If not RET:

Logging.info ("Can ' t get lock! RET:%s ", ret)

Return

Logging.info ("Get lock! Do something! Sleep Ten secs! ")

For I in range (1, 11):

Time.sleep (1)

Print str (i)

Lock.release ()

if __name__ = = "__main__":

Try

Main ()

Except Exception, ex:

Print "ocurred Exception:%s"% str (ex)

Quit ()

Copy the test file to more than one server and run it, you can see the effect of the distributed lock.

Reference Links:

Http://kazoo.readthedocs.org/en/latest/basic_usage.html

http://yunjianfei.iteye.com/blog/2164888

Zookeeper in-depth understanding (iii) Kazoo interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.