本文整理汇总了Python中lock.Lock类的典型用法代码示例。如果您正苦于以下问题:Python Lock类的具体用法?Python Lock怎么用?Python Lock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Lock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: detachThin
def detachThin(session, lvmCache, srUuid, vdiUuid):
"""Shrink the VDI to the minimal size if no one is using it"""
lvName = LV_PREFIX[vhdutil.VDI_TYPE_VHD] + vdiUuid
path = os.path.join(VG_LOCATION, VG_PREFIX + srUuid, lvName)
lock = Lock(vhdutil.LOCK_TYPE_SR, srUuid)
_tryAcquire(lock)
vdiRef = session.xenapi.VDI.get_by_uuid(vdiUuid)
vbds = session.xenapi.VBD.get_all_records_where( \
"field \"VDI\" = \"%s\"" % vdiRef)
numPlugged = 0
for vbdRec in vbds.values():
if vbdRec["currently_attached"]:
numPlugged += 1
if numPlugged > 1:
raise util.SMException("%s still in use by %d others" % \
(vdiUuid, numPlugged - 1))
lvmCache.activate(NS_PREFIX_LVM + srUuid, vdiUuid, lvName, False)
try:
newSize = calcSizeLV(vhdutil.getSizePhys(path))
deflate(lvmCache, lvName, newSize)
finally:
lvmCache.deactivate(NS_PREFIX_LVM + srUuid, vdiUuid, lvName, False)
lock.release()
示例2: daemonize
def daemonize(args, callback):
with DaemonContext():
create_process = False
lock = Lock(LOCKFILE, os.getpid(), args.name, args.sea_ep[0],
args.sea_ep[1], args.port)
if lock.is_locked():
lock_pid = lock.get_pid()
if not lock.is_same_file(args.name, args.sea_ep[0],
args.sea_ep[1]) \
or not is_process_running(lock_pid):
try:
os.kill(lock_pid, signal.SIGQUIT)
except OSError:
pass
except TypeError:
pass
lock.break_lock()
create_process = True
else:
create_process = True
if create_process:
lock.acquire()
callback(args.name, season=args.sea_ep[0], episode=args.sea_ep[1],
serve=True, port=args.port)
lock.release()
示例3: PhysicalView
class PhysicalView(Base):
def __init__(self, dfs):
Base.__init__(self, dfs)
self.lock_ = Lock(dfs)
self.createBaseFolder()
def read(self, fileName, buf, offset, bufsize):
# TODO add thread safetly
filePath = os.path.join(self.getBasePath(), fileName)
size = self.getFileSize(fileName)
if offset + bufsize > size:
self.log_.w('tried to read ' + fileName + ' but size is ' + str(size) +
' and bufsize + offset = ' + str(offset + bufsize))
return err.InvalidBufferSize
self.lock_.acquire()
try:
f = open(filePath, "r")
except Exception, ex:
self.log_.e('error opening file in read mode ' + filePath + ': ' + str(ex))
self.lock_.release()
return err.FileNotFound
status = err.OK
f.seek(offset)
try:
data = f.read(bufsize)
for i, d in enumerate(data):
buf[i] = d
except Exception, ex:
self.log_.e('failed to read ' + filePath + ' from ' + str(offset) + ' to ' + str(offset + bufsize) + ': ' + str(ex))
status = err.CannotReadFile
示例4: Test02_MfcLock
class Test02_MfcLock(unittest.TestCase):
'''Unittest for mfc1.Lock().'''
def setUp(self):
'''Prepare test.'''
self.__lock = Lock('./', 'andy')
def test_get_pid(self):
'''Testing mfc1.Lock.get_pid().'''
r = self.__lock.get_pid('mindfulclock1')
self.failUnlessEqual(first=r, second=0)
def test_one_instance(self):
'''Testing mfc1.Lock.one_instance().'''
r = self.__lock.one_instance('mindfulclock1')
self.failUnlessEqual(first=r, second=True)
def test_write_check_delete_lock(self):
'''Testing write_lock(), check_lock(), delete_lock().'''
r = self.__lock.write_lock()
self.failUnlessEqual(first=r, second=True)
# chek lock file.
r = self.__lock.is_lock()
self.failUnlessEqual(first=r, second=True)
# delete lock file.
r = self.__lock.delete_lock()
self.failUnlessEqual(first=r, second=True)
示例5: Test02_MfcLock
class Test02_MfcLock(unittest.TestCase):
'''Unittest for mfc1.Lock().'''
def setUp(self):
'''Prepare test.'''
self.__lock = Lock('./', 'andy')
def test_delete_lock(self):
'''Testing mfc1.Lock.delete_lock().'''
r = self.__lock.delete_lock()
self.failUnlessEqual(first=r, second=False)
def test_is_lock(self):
'''Testing mfc1.Lock.is_lock().'''
r = self.__lock.is_lock()
self.failUnlessEqual(first=r, second=False)
def test_write_lock(self):
'''Testing mfc1.Lock.write_lock().'''
r = self.__lock.write_lock()
self.failUnlessEqual(first=r, second=True)
# chek lock file.
r = self.__lock.is_lock()
self.failUnlessEqual(first=r, second=True)
# delete lock file.
r = self.__lock.delete_lock()
self.failUnlessEqual(first=r, second=True)
示例6: Test02_MfcLock
class Test02_MfcLock(unittest.TestCase):
"""Unittest for mfc1.Lock()."""
def setUp(self):
"""Prepare test."""
self.lock = Lock("./", "andy")
def test_get_pid(self):
"""Testing mfc1.Lock.get_pid()."""
r = self.lock.get_pid("mindfulclock1")
self.failUnlessEqual(first=r, second=0)
def test_one_instance(self):
"""Testing mfc1.Lock.one_instance()."""
r = self.lock.one_instance("mindfulclock1")
self.failUnlessEqual(first=r, second=True)
def test_write_check_delete_lock(self):
"""Testing write_lock(), check_lock(), delete_lock()."""
r = self.lock.write_lock()
self.failUnlessEqual(first=r, second=True)
# chek lock file.
r = self.lock.is_lock()
self.failUnlessEqual(first=r, second=True)
# delete lock file.
r = self.lock.delete_lock()
self.failUnlessEqual(first=r, second=True)
示例7: daemonize
def daemonize(args, callback):
with DaemonContext():
from touchandgo.logger import log_set_up
log_set_up(True)
log = logging.getLogger('touchandgo.daemon')
log.info("running daemon")
create_process = False
lock = Lock(LOCKFILE, os.getpid(), args.name, args.sea_ep[0],
args.sea_ep[1], args.port)
if lock.is_locked():
log.debug("lock active")
lock_pid = lock.get_pid()
if not lock.is_same_file(args.name, args.sea_ep[0],
args.sea_ep[1]) \
or not is_process_running(lock_pid):
try:
log.debug("killing process %s" % lock_pid)
os.kill(lock_pid, signal.SIGQUIT)
except OSError:
pass
except TypeError:
pass
lock.break_lock()
create_process = True
else:
create_process = True
if create_process:
log.debug("creating proccess")
lock.acquire()
callback()
lock.release()
else:
log.debug("same daemon process")
示例8: checkLocked
def checkLocked(obj, ns):
"""Lock-protected access"""
lock = Lock(obj, ns)
lock.acquire()
try:
return RefCounter.check(obj, ns)
finally:
lock.release()
示例9: setReadonly
def setReadonly(self, lvName, readonly):
path = self._getPath(lvName)
if self.lvs[lvName].readonly != readonly:
uuids = util.findall_uuid(path)
ns = lvhdutil.NS_PREFIX_LVM + uuids[0]
lock = Lock(uuids[1], ns)
lock.acquire()
lvutil.setReadonly(path, readonly)
lock.release()
self.lvs[lvName].readonly = readonly
示例10: setReadonly
def setReadonly(self, lvName, readonly):
path = self._getPath(lvName)
if self.lvs[lvName].readonly != readonly:
uuids = util.findall_uuid(path)
ns = lvhdutil.NS_PREFIX_LVM + uuids[0]
# Taking this lock is needed to avoid a race condition
# with tap-ctl open (which is now taking the same lock)
lock = Lock("lvchange-p", ns)
lock.acquire()
lvutil.setReadonly(path, readonly)
lock.release()
self.lvs[lvName].readonly = readonly
示例11: activate
def activate(self, ns, ref, lvName, binary):
lock = Lock(ref, ns)
lock.acquire()
try:
count = RefCounter.get(ref, binary, ns)
if count == 1:
try:
self.activateNoRefcount(lvName)
except util.CommandException:
RefCounter.put(ref, binary, ns)
raise
finally:
lock.release()
示例12: deactivate
def deactivate(self, sr_uuid, vdi_uuid):
"""Deactivate VDI - called post tapdisk close"""
if self._get_blocktracking_status():
from lock import Lock
lock = Lock("cbtlog", str(vdi_uuid))
lock.acquire()
try:
logpath = self._get_cbt_logpath(vdi_uuid)
logname = self._get_cbt_logname(vdi_uuid)
self._cbt_op(vdi_uuid, cbtutil.set_cbt_consistency, logpath, True)
# Finally deactivate log file
self._deactivate_cbt_log(logname)
finally:
lock.release()
示例13: _cbt_op
def _cbt_op(self, uuid, func, *args):
# Lock cbtlog operations
from lock import Lock
lock = Lock("cbtlog", str(uuid))
lock.acquire()
try:
logname = self._get_cbt_logname(uuid)
activated = self._activate_cbt_log(logname)
ret = func(*args)
if activated:
self._deactivate_cbt_log(logname)
return ret
finally:
lock.release()
示例14: __init__
def __init__(self, config):
"""A valid individual able to run tasks.
Must be validated as a prerequisite
Args:
config: a self.config ConfigParser object.
"""
self.__config = config
self.__subjectDir = self.__config.get('arguments', 'subjectDir')
self.__name = os.path.basename(self.__subjectDir)
self.__logDir = os.path.join(self.__subjectDir, self.__config.get('dir', 'log'))
if not os.path.exists(self.__logDir):
os.mkdir(self.__logDir)
Lock.__init__(self, self.__logDir, self.__name)
示例15: _kickGC
def _kickGC(self):
# don't bother if an instance already running (this is just an
# optimization to reduce the overhead of forking a new process if we
# don't have to, but the process will check the lock anyways)
lockRunning = Lock(cleanup.LOCK_TYPE_RUNNING, self.uuid)
if not lockRunning.acquireNoblock():
if cleanup.should_preempt(self.session, self.uuid):
util.SMlog("Aborting currently-running coalesce of garbage VDI")
try:
if not cleanup.abort(self.uuid, soft=True):
util.SMlog("The GC has already been scheduled to " "re-start")
except util.CommandException, e:
if e.code != errno.ETIMEDOUT:
raise
util.SMlog("failed to abort the GC")
finally: