基于zookeeper的分布式Lock及RLock的实现
一.Lock and RLock 需要zookeeper的python 客户端kazoo # -*- coding: utf-8 -*- import os import uuid from kazoo.client import KazooClient from kazoo.recipe.lock import Lock class ZkLock(object): def __init__(self,lockname,zkhosts,basepath='/locks',logger=None,timeout=5): self._zk_hosts = zkhosts # self._client = KazooClient(hosts=self._zk_hosts,logger=logger,timeout=timeout) self._zk_path = os.path.join(basepath,lockname) self._lock_handle = Lock(self._client,self._zk_path) def start(self): self._client.start() def stop(self): self._client.stop() def acquire(self, blocking=True, timeout=None): self