本文整理汇总了Python中misc.filetool.lock函数的典型用法代码示例。如果您正苦于以下问题:Python lock函数的具体用法?Python lock怎么用?Python lock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了lock函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: write
def write(self, cacheId, content, memory=False, writeToFile=True, keepLock=False):
filetool.directory(self._path)
cacheFile = os.path.join(self._path, self.filename(cacheId))
if writeCond(cacheId):
print "\nWriting %s ..." % (cacheId,),
if writeToFile:
try:
if not cacheFile in self._locked_files:
self._locked_files.add(cacheFile) # this is not atomic with the next one!
filetool.lock(cacheFile)
fobj = open(cacheFile, 'wb')
pickle.dump(content, fobj, 2)
fobj.close()
if not keepLock:
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile) # not atomic with the previous one!
#print "wrote cacheId: %s" % cacheId
if writeCond(cacheId):
print "to disk"
except (IOError, EOFError, pickle.PickleError, pickle.PicklingError), e:
e.args = ("Could not store cache to %s\n" % self._path + e.args[0], ) + e.args[1:]
raise e
开发者ID:unify,项目名称:qooxdoo,代码行数:28,代码来源:Cache.py
示例2: read
def read(self, cacheId, dependsOn=None, memory=False, keepLock=False):
if dependsOn:
dependsModTime = os.stat(dependsOn).st_mtime
# Mem cache
if cacheId in memcache:
memitem = memcache[cacheId]
if not dependsOn or dependsModTime < memitem['time']:
return memitem['content'], memitem['time']
# File cache
cacheFile = os.path.join(self._path, self.filename(cacheId))
try:
cacheModTime = os.stat(cacheFile).st_mtime
except OSError:
return None, None
# out of date check
if dependsOn and dependsModTime > cacheModTime:
return None, cacheModTime
try:
try:
if not cacheFile in self._locked_files:
self._locked_files.add(cacheFile)
filetool.lock(cacheFile)
fobj = open(cacheFile, 'rb')
fcontent = fobj.read().decode('zlib')
fobj.close()
finally:
if not keepLock:
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile)
except (IOError, ):
self._console.warn("Could not read cache object %s" % cacheFile)
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile)
return None, cacheModTime
try:
gc.disable()
try:
content = pickle.loads(fcontent)
finally:
gc.enable()
if memory:
memcache[cacheId] = {'content':content, 'time': time.time()}
#print "read cacheId: %s" % cacheId
return content, cacheModTime
except (EOFError, pickle.PickleError, pickle.UnpicklingError):
self._console.warn("Could not unpickle cache object %s" % cacheFile)
return None, cacheModTime
开发者ID:aparusel,项目名称:qooxdoo,代码行数:58,代码来源:Cache.py
示例3: read
def read(self, cacheId, dependsOn=None, memory=False, keepLock=False):
if dependsOn:
dependsModTime = os.stat(dependsOn).st_mtime
if writeCond(cacheId):
print "\nReading %s ..." % (cacheId,),
# Mem cache
if cacheId in memcache:
memitem = memcache[cacheId]
if not dependsOn or dependsModTime < memitem['time']:
if writeCond(cacheId):
print "from memcache"
return memitem['content'], memitem['time']
# File cache
filetool.directory(self._path)
cacheFile = os.path.join(self._path, self.filename(cacheId))
try:
cacheModTime = os.stat(cacheFile).st_mtime
except OSError:
return None, None
# out of date check
if dependsOn and dependsModTime > cacheModTime:
return None, cacheModTime
try:
if not cacheFile in self._locked_files:
self._locked_files.add(cacheFile)
filetool.lock(cacheFile)
fobj = open(cacheFile, 'rb')
#filetool.lock(fobj.fileno())
content = pickle.load(fobj)
#filetool.unlock(fobj.fileno())
fobj.close()
if not keepLock:
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile)
if memory:
memcache[cacheId] = {'content':content, 'time': time.time()}
#print "read cacheId: %s" % cacheId
if writeCond(cacheId):
print "from disk"
return content, cacheModTime
except (IOError, EOFError, pickle.PickleError, pickle.UnpicklingError):
self._console.error("Could not read cache from %s" % self._path)
return None, cacheModTime
开发者ID:unify,项目名称:qooxdoo,代码行数:54,代码来源:Cache.py
示例4: write
def write(self, cacheId, content, memory=False, writeToFile=True):
filetool.directory(self._path)
cacheFile = os.path.join(self._path, self.filename(cacheId))
if writeToFile:
try:
self._locked_files.add(cacheFile) # this is not atomic with the next one!
filetool.lock(cacheFile)
fobj = open(cacheFile, 'wb')
pickle.dump(content, fobj, 2)
fobj.close()
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile) # not atomic with the previous one!
except (IOError, EOFError, pickle.PickleError, pickle.PicklingError), e:
e.args = ("Could not store cache to %s\n" % self._path + e.args[0], ) + e.args[1:]
raise e
开发者ID:carriercomm,项目名称:EyeOS,代码行数:19,代码来源:Cache.py
示例5: read
def read(self, cacheId, dependsOn=None, memory=False):
if memcache.has_key(cacheId):
return memcache[cacheId]
filetool.directory(self._path)
cacheFile = os.path.join(self._path, self.filename(cacheId))
try:
cacheModTime = os.stat(cacheFile).st_mtime
except OSError:
return None
# Out of date check
if dependsOn:
fileModTime = os.stat(dependsOn).st_mtime
if fileModTime > cacheModTime:
return None
try:
self._locked_files.add(cacheFile)
filetool.lock(cacheFile)
fobj = open(cacheFile, 'rb')
#filetool.lock(fobj.fileno())
content = pickle.load(fobj)
#filetool.unlock(fobj.fileno())
fobj.close()
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile)
if memory:
memcache[cacheId] = content
return content
except (IOError, EOFError, pickle.PickleError, pickle.UnpicklingError):
self._console.error("Could not read cache from %s" % self._path)
return None
开发者ID:mikegr,项目名称:lectorious-grails-qooxdoo,代码行数:40,代码来源:Cache.py
示例6: write
def write(self, cacheId, content, memory=False, writeToFile=True, keepLock=False):
cacheFile = os.path.join(self._path, self.filename(cacheId))
if writeToFile:
try:
if not cacheFile in self._locked_files:
self._locked_files.add(cacheFile) # this is not atomic with the next one!
filetool.lock(cacheFile)
fobj = open(cacheFile, 'wb')
fobj.write(pickle.dumps(content, 2).encode('zlib'))
fobj.close()
if not keepLock:
filetool.unlock(cacheFile)
self._locked_files.remove(cacheFile) # not atomic with the previous one!
except (IOError, EOFError, pickle.PickleError, pickle.PicklingError), e:
try:
os.unlink(cacheFile) # try remove cache file, Pickle might leave incomplete files
except:
e.args = ("Cache file might be crippled.\n" % self._path + e.args[0], ) + e.args[1:]
e.args = ("Could not store cache to %s.\n" % self._path + e.args[0], ) + e.args[1:]
raise e
开发者ID:reneolivo,项目名称:qooxdoo,代码行数:24,代码来源:Cache.py
注:本文中的misc.filetool.lock函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论