本文整理汇总了Python中portalocker.unlock函数的典型用法代码示例。如果您正苦于以下问题:Python unlock函数的具体用法?Python unlock怎么用?Python unlock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unlock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, request, folder=None):
self.request = request
# Lets test if the cache folder exists, if not
# we are going to create it
folder = folder or os.path.join(request.folder, "cache")
if not os.path.exists(folder):
os.mkdir(folder)
### we need this because of a possible bug in shelve that may
### or may not lock
self.locker_name = os.path.join(request.folder, "cache/cache.lock")
self.shelve_name = os.path.join(request.folder, "cache/cache.shelve")
locker, locker_locked = None, False
try:
locker = open(self.locker_name, "a")
portalocker.lock(locker, portalocker.LOCK_EX)
locker_locked = True
storage = shelve.open(self.shelve_name)
if not storage.has_key(CacheAbstract.cache_stats_name):
storage[CacheAbstract.cache_stats_name] = {"hit_total": 0, "misses": 0}
storage.sync()
except ImportError:
pass # no module _bsddb, ignoring exception now so it makes a ticket only if used
except:
logger.error("corrupted file: %s" % self.shelve_name)
if locker_locked:
portalocker.unlock(locker)
if locker:
locker.close()
示例2: findT
def findT(path, language='en-us'):
"""
must be run by the admin app
"""
filename = os.path.join(path, 'languages', '%s.py' % language)
sentences = read_dict(filename)
mp = os.path.join(path, 'models')
cp = os.path.join(path, 'controllers')
vp = os.path.join(path, 'views')
for file in listdir(mp, '.+\.py', 0) + listdir(cp, '.+\.py', 0)\
+ listdir(vp, '.+\.html', 0):
fp = open(file, 'r')
portalocker.lock(fp, portalocker.LOCK_SH)
data = fp.read()
portalocker.unlock(fp)
fp.close()
items = regex_translate.findall(data)
for item in items:
try:
message = eval(item)
if not message.startswith('#') and not '\n' in message:
tokens = message.rsplit('##', 1)
else:
# this allows markmin syntax in translations
tokens = [message]
if len(tokens) == 2:
message = tokens[0].strip() + '##' + tokens[1].strip()
if message and not message in sentences:
sentences[message] = message
except:
pass
write_dict(filename, sentences)
示例3: findT
def findT(path, language="en-us"):
"""
must be run by the admin app
"""
filename = os.path.join(path, "languages", "%s.py" % language)
sentences = read_dict(filename)
mp = os.path.join(path, "models")
cp = os.path.join(path, "controllers")
vp = os.path.join(path, "views")
for file in listdir(mp, ".+\.py", 0) + listdir(cp, ".+\.py", 0) + listdir(vp, ".+\.html", 0):
fp = open(file, "r")
portalocker.lock(fp, portalocker.LOCK_SH)
data = fp.read()
portalocker.unlock(fp)
fp.close()
items = regex_translate.findall(data)
for item in items:
try:
message = eval(item)
if not message.startswith("#") and not "\n" in message:
tokens = message.rsplit("##", 1)
else:
# this allows markmin syntax in translations
tokens = [message]
if len(tokens) == 2:
message = tokens[0].strip() + "##" + tokens[1].strip()
if message and not message in sentences:
sentences[message] = message
except:
pass
write_dict(filename, sentences)
示例4: planet
def planet():
#return ""
import gluon.contrib.rss2 as rss2
# store planet rss entries in disk (forever...)
import portalocker
import os, cPickle as pickle
path = os.path.join(request.folder,'cache', "planet.rss")
if not os.path.exists(path):
f = open(path, "w+")
rss = get_planet_rss(None)
rss = [{'title': item.title, 'author': item.author, 'pubDate': item.pubDate, 'link': item.link, 'description': item.description} for item in rss.items]
else:
f = open(path, "r+")
rss = None
portalocker.lock(f, portalocker.LOCK_EX)
if not rss:
rss = pickle.load(f)
else:
f.seek(0)
pickle.dump(rss, f)
portalocker.unlock(f)
f.close()
# .rss requests
if request.extension == "rss":
# return new rss feed xml
response.headers['Content-Type']='application/rss+xml'
return rss2.dumps(rss)
# else send the rss object to be processed by
# the view
return response.render(dict(rss = rss, rss2 = rss2))
示例5: load_storage
def load_storage(filename):
fp = open(filename, 'rb')
portalocker.lock(fp, portalocker.LOCK_EX)
storage = cPickle.load(fp)
portalocker.unlock(fp)
fp.close()
return Storage(storage)
示例6: unlock
def unlock(self,key):
try: portalocker.unlock(self.locked[key])
except: print "Cache error unlocking file with key " + key
try: self.locked[key].close()
except: print "Cache error closing file with key " + key
del self.open[key]
del self.locked[key]
示例7: __getCachedData
def __getCachedData(self,key):
fname = self.__fname(key)
# Pre-existing locked read
if(self.locked.get(key)):
self.locked[key].seek(0)
try: str = pickle.load(self.locked[key])
except: str = False
self.locked[key].seek(0)
return str
fp=open(fname, "r")
self.open[key] = fp
portalocker.lock(fp,portalocker.LOCK_SH)
# The following 2 lines handle cases where open (above) was called
# on an empty file that was created by cache::lock()
fp.seek(0)
try: str = pickle.load(fp)
except: str = False
try: portalocker.unlock(fp)
except: print "Cache error unlocking file with key " + key
try: fp.close()
except: print "Cache error closing file with key " + key
del self.open[key]
return str
示例8: _open_shelve_and_lock
def _open_shelve_and_lock(self):
"""Open and return a shelf object, obtaining an exclusive lock
on self.locker first. Replaces the close method of the
returned shelf instance with one that releases the lock upon
closing."""
storage = None
locker = None
locked = False
try:
locker = locker = open(self.locker_name, 'a')
portalocker.lock(locker, portalocker.LOCK_EX)
locked = True
try:
storage = shelve.open(self.shelve_name)
except:
logger.error('corrupted cache file %s, will try rebuild it' \
% (self.shelve_name))
storage = None
if not storage and os.path.exists(self.shelve_name):
os.unlink(self.shelve_name)
storage = shelve.open(self.shelve_name)
if not CacheAbstract.cache_stats_name in storage.keys():
storage[CacheAbstract.cache_stats_name] = {'hit_total':0, 'misses': 0}
storage.sync()
except Exception, e:
if storage:
storage.close()
storage = None
if locked:
portalocker.unlock(locker)
locker.close()
locked = False
raise RuntimeError, 'unable to create/re-create cache file %s' % self.shelve_name
示例9: myunlock
def myunlock(myfilename):
lock_file = portalock_open(myfilename)
lock_file.truncate()
lock_file.write('clear\n')
lock_file.flush()
portalocker.unlock(lock_file)
lock_file.close()
示例10: __call__
def __call__(
self,
key,
f,
time_expire = DEFAULT_TIME_EXPIRE,
):
dt = time_expire
locker = open(self.locker_name,'a')
portalocker.lock(locker, portalocker.LOCK_EX)
storage = shelve.open(self.shelve_name)
item = storage.get(key, None)
if item and f == None:
del storage[key]
portalocker.unlock(locker)
locker.close()
if f is None:
return None
if item and (dt == None or item[0] > time.time() - dt):
return item[1]
value = f()
locker = open(self.locker_name,'a')
portalocker.lock(locker, portalocker.LOCK_EX)
storage[key] = (time.time(), value)
storage.sync()
portalocker.unlock(locker)
locker.close()
return value
示例11: findT
def findT(path, language='en-us'):
"""
must be run by the admin app
"""
filename = os.path.join(path, 'languages', '%s.py' % language)
sentences = read_dict(filename)
mp = os.path.join(path, 'models')
cp = os.path.join(path, 'controllers')
vp = os.path.join(path, 'views')
for file in listdir(mp, '.+\.py', 0) + listdir(cp, '.+\.py', 0)\
+ listdir(vp, '.+\.html', 0):
fp = open(file, 'r')
portalocker.lock(fp, portalocker.LOCK_SH)
data = fp.read()
portalocker.unlock(fp)
fp.close()
items = regex_translate.findall(data)
for item in items:
try:
msg = eval(item)
if msg and not msg in sentences:
sentences[msg] = msg
except:
pass
write_dict(filename, sentences)
示例12: test_simple
def test_simple():
fh = open('tests/test_file.txt', 'r+')
portalocker.lock(fh, portalocker.LOCK_EX)
fh.seek(12)
fh.write('foo')
portalocker.unlock(fh)
fh.close()
示例13: from_conf
def from_conf(cls, path=None, **overrides):
'''Initialize instance from YAML configuration file,
writing updates (only to keys, specified by "conf_update_keys") back to it.'''
import yaml
if path is None:
path = cls.conf_path_default
log.debug('Using default state-file path: {}'.format(path))
path = os.path.expanduser(path)
with open(path, 'r') as src:
portalocker.lock(src, portalocker.LOCK_SH)
# fcntl.lockf(src, fcntl.LOCK_SH)
conf = yaml.load(src.read())
portalocker.unlock(src)
conf.setdefault('conf_save', path)
conf_cls = dict()
for ns, keys in cls.conf_update_keys.viewitems():
for k in keys:
try:
v = conf.get(ns, dict()).get(k)
except AttributeError:
if not cls.conf_raise_structure_errors: raise
raise KeyError('Unable to get value for configuration parameter'
' "{k}" in section "{ns}", check configuration file (path: {path}) syntax'
' near the aforementioned section/value.'.format(ns=ns, k=k, path=path))
if v is not None:
conf_cls['{}_{}'.format(ns, k)] = conf[ns][k]
conf_cls.update(overrides)
self = cls(**conf_cls)
self.conf_save = conf['conf_save']
return self
示例14: _unlock
def _unlock(self):
response = current.response
if response and getattr(response,'session_file',None) and getattr(response,'session_locked',None):
try:
portalocker.unlock(response.session_file)
response.session_locked = False
except: # this should never happen but happens in Windows
pass
示例15: progress_bar
def progress_bar(current, left):
with open('progress_bar.lock', 'w') as lockfile:
portalocker.lock(lockfile, portalocker.LOCK_EX)
progress = 120 * (current/left)
stringbuilder = '[{0}] {1}%'.format('#' * (progress / 12), progress * 120 * 100)
lockfile.write(stringbuilder)
say(stringbuilder)
portalocker.unlock(lockfile)