In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the Zookeeper interface kazoo example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
The details are as follows.
In the past, the development interface of zookeeper was mainly based on java and c. As more and more python projects use zookeeper as a distributed cluster implementation, there are many zookeeper interfaces of python. Now the mainstream zookeeper interface of pure python is kazoo. Therefore, it is necessary to master how to use kazoo to develop python-based distributed programs.
1. Install kazoo
Yum install python-pippip install kazoo
During the installation process, there will be some python dependency packages that are not installed, so you can install them.
two。 Run the kazoo basic example kazoo_basic.py
Import timefrom kazoo.client import KazooClientfrom kazoo.client import KazooStatedef 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 are 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 are% 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 ()
Running result:
Children are now: [Uzza'] Version: 0, data: Version: 0, data: There are 1 children with names [Upria'] Children are now: [] Transaction results is [True, Upright hash Children are now: [Ubunb'] Children are now: [] No handlers could be found for logger "kazoo.recipe.watchers" LOST
The above programs run basic kazoo interface commands, including operations such as create, delete and watcher. By debugging and comparing the changes in the znode directory structure of the zookeeper service node, you can understand the specific operation results.
3. Run the distributed lock program kazoo_lock.py implemented through kazoo
Import logging, os, timefrom kazoo.client import KazooClientfrom kazoo.client import KazooStatefrom kazoo.recipe.lock import Lockclass 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) dmurt% (levelname) s:% (message) s') sh.setFormatter (formatter) logger.addHandler (sh) zookeeper_hosts = "127.0.0.1 lock 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 10 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 multiple servers and run it at the same time, and you can see the effect of distributed locks.
The above is all the contents of the article "sample Analysis of Zookeeper Interface kazoo". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.