本文整理汇总了Python中MaKaC.common.utils.OSSpecific.lockFile方法的典型用法代码示例。如果您正苦于以下问题:Python OSSpecific.lockFile方法的具体用法?Python OSSpecific.lockFile怎么用?Python OSSpecific.lockFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MaKaC.common.utils.OSSpecific
的用法示例。
在下文中一共展示了OSSpecific.lockFile方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getCachePage
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def getCachePage( self ):
if os.path.isfile(self.getFilePath()):
fp = open(self.getFilePath(),"r")
OSSpecific.lockFile(fp, 'LOCK_SH')
page = fp.read()
return page
return ""
示例2: set
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def set(self, key, val, ttl=0):
f = open(self._getFilePath(key), 'wb')
OSSpecific.lockFile(f, 'LOCK_EX')
try:
expiry = int(time.time()) + ttl if ttl else None
data = (expiry, val)
pickle.dump(data, f)
finally:
OSSpecific.lockFile(f, 'LOCK_UN')
f.close()
return 1
示例3: lockCache
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def lockCache( self, file, flag=True):
global fp
if flag:
fp = open(file,"a")
OSSpecific.lockFile(fp, 'LOCK_EX')
else:
if not fp:
return
OSSpecific.lockFile(fp, 'LOCK_UN')
fp.close()
fp = None
示例4: save
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def save(self, path, name, data):
fsPath = os.path.join(self._dir, path)
if not os.path.exists(fsPath):
os.makedirs(fsPath)
filePath = os.path.join(fsPath, name)
f = open(filePath, 'wb')
OSSpecific.lockFile(f, 'LOCK_EX')
try:
pickle.dump(data, f)
finally:
OSSpecific.lockFile(f, 'LOCK_UN')
f.close()
示例5: load
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def load(self, path, name, default=None):
filePath = os.path.join(self._dir, path, name)
if not os.path.exists(filePath):
return default, None
f = open(filePath, 'rb')
OSSpecific.lockFile(f, 'LOCK_SH')
try:
obj = pickle.load(f)
mtime = os.path.getmtime(filePath)
finally:
OSSpecific.lockFile(f, 'LOCK_UN')
f.close()
return obj, mtime
示例6: get
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def get(self, key):
path = self._getFilePath(key)
if not os.path.exists(path):
return None
f = open(path, 'rb')
OSSpecific.lockFile(f, 'LOCK_SH')
expiry = val = None
try:
expiry, val = pickle.load(f)
finally:
OSSpecific.lockFile(f, 'LOCK_UN')
f.close()
if expiry and time.time() > expiry:
return None
return val
示例7: set
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def set(self, key, val, ttl=0):
try:
f = open(self._getFilePath(key), 'wb')
OSSpecific.lockFile(f, 'LOCK_EX')
try:
expiry = int(time.time()) + ttl if ttl else None
data = (expiry, val)
pickle.dump(data, f)
finally:
OSSpecific.lockFile(f, 'LOCK_UN')
f.close()
except (IOError, OSError):
Logger.get('FileCache').exception('Error setting value in cache')
return 0
return 1
示例8: get
# 需要导入模块: from MaKaC.common.utils import OSSpecific [as 别名]
# 或者: from MaKaC.common.utils.OSSpecific import lockFile [as 别名]
def get(self, key):
try:
path = self._getFilePath(key)
if not os.path.exists(path):
return None
f = open(path, 'rb')
OSSpecific.lockFile(f, 'LOCK_SH')
expiry = val = None
try:
expiry, val = pickle.load(f)
finally:
OSSpecific.lockFile(f, 'LOCK_UN')
f.close()
if expiry and time.time() > expiry:
return None
except (IOError, OSError):
Logger.get('FileCache').exception('Error getting cached value')
return None
except (EOFError, pickle.UnpicklingError):
Logger.get('FileCache').exception('Cached information seems corrupted. Overwriting it.')
return None
return val