本文整理汇总了Python中tools.locker.locker.lock函数的典型用法代码示例。如果您正苦于以下问题:Python lock函数的具体用法?Python lock怎么用?Python lock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了lock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_user_role
def get_user_role(self, username, email=None):
with locker.lock(username):
if username not in self.__users_roles:
if not self.__db.document_exists(username):
self.__users_roles[username] = 'user'
else:
user = self.__db.get(username)
if email and ('email' not in user or user['email'] != email):
user['email'] = email
self.__db.update(user)
role = None
while not role:
try:
role = user['role']
except:
## how to print an error from here ?
user = self.__db.get(username)
pass
self.__users_roles[username] = user['role']
else:
if self.__lookup_counter[username] == settings().get_value("user_cache_refresh_counter"):
if self.__db.document_exists(username):
self.__users_roles[username] = self.__db.get(username)['role']
self.__lookup_counter[username] = 0
else:
self.__lookup_counter[username] += 1
return self.__users_roles[username]
示例2: overwrite
def overwrite(self, json_input):
"""
Update the document with the input, regardless of revision clash.
This has to be used to much care
"""
try:
if self.__class__.__name__ =="batch":
db = database(self.__class__.__name__ + "es")
else:
db = database(self.__class__.__name__ + "s")
except (database.DatabaseNotFoundException, database.DatabaseAccessError) as ex:
self.logger.error("Problem with database creation:\n{0}".format(ex))
return False
with locker.lock(self.get_attribute('_id')):
if not db.document_exists(self.get_attribute('_id')):
return False
## reload the doc with db
self.__init__(db.get(self.get_attribute('_id')))
## add what was provided on top
self._json_base__json.update( json_input )
## save back
saved = db.update(self.json())
if not saved:
return False
return True
示例3: overwrite
def overwrite(self, json_input):
"""
Update the document with the input, regardless of revision clash.
This has to be used to much care
"""
db = self.get_database()
if db is None:
return False
with locker.lock(self.get_attribute('_id')):
if not db.document_exists(self.get_attribute('_id')):
return False
# reload the doc with db
t = db.get(self.get_attribute('_id'))
self.__init__(t)
if "_rev" in json_input:
self.logger.debug("trying to overwrite.DB _rev:%s Doc _rev: %s" % (t["_rev"], json_input["_rev"]))
else:
self.logger.debug("trying to overwrite.DB _rev:%s Doc _rev: none" % (t["_rev"]))
# add what was provided on top
self._json_base__json.update(json_input)
# save back
saved = db.update(self.json())
if not saved:
return False
return True
示例4: add
def add(label, setting):
with locker.lock(label):
result = __db.save(setting)
if result:
cache_key = 'settings_' + label
__cache.set(cache_key, setting)
return result
示例5: __get_from_cache
def __get_from_cache(self, key):
if self.cache_enabled:
with locker.lock(key):
cache_key = 'mcm_database_' + key
return self.cache.get(cache_key)
else:
return None
示例6: default
def default(self, *vpath, **params):
method = getattr(self, cherrypy.request.method, None)
if not method:
raise cherrypy.HTTPError(405, "Method not implemented.")
if self.access_limit is not None:
self.logger.log('Setting access limit to access_rights.%s (%s)' % (roles[self.access_limit], self.access_limit))
self.authenticator.set_limit(self.access_limit)
elif cherrypy.request.method in self.limit_per_method:
self.authenticator.set_limit(self.limit_per_method[cherrypy.request.method])
else:
raise cherrypy.HTTPError(403, 'You cannot access this page with method %s' % cherrypy.request.method )
user_p = user_pack()
l_type = locator()
if not user_p.get_username():
#meaning we are going public, only allow GET.
#if cherrypy.request.method != 'GET' or not l_type.isDev():
# raise cherrypy.HTTPError(403, 'User credentials were not provided.')
if not 'public' in str(cherrypy.url()):
self.logger.error('From within %s, adfs-login not found: \n %s \n %s' % (self.__class__.__name__, str(cherrypy.request.headers), str(cherrypy.url()) ))
else:
if not self.authenticator.can_access(user_p.get_username()):
raise cherrypy.HTTPError(403, 'You cannot access this page, the limit for the page is {0} ({1})'.format(roles[self.authenticator.get_limit()],
self.authenticator.get_limit()))
# counter for calls
with locker.lock("rest-call-counter"):
self.counter[method.im_class.__name__][method.__name__] += 1
return method(*vpath, **params)
示例7: toggle_last_request
def toggle_last_request(self):
## let it toggle the last request to a given approval only if the chained request allows it
if self.get_attribute('approval') == 'none':
return
ccdb = database('chained_campaigns')
mcm_cc = ccdb.get(self.get_attribute('member_of_campaign'))
(next_campaign_id, flow_name) = mcm_cc['campaigns'][self.get_attribute('step')]
fdb = database('flows')
mcm_f = flow(fdb.get(flow_name))
# check whether we have to do something even more subtle with the request
if mcm_f.get_attribute('approval') == 'submit' or self.get_attribute('approval') == 'submit':
rdb = database('requests')
next_request = request(rdb.get(self.get_attribute('chain')[self.get_attribute('step')]))
current_r_approval = next_request.get_attribute('approval')
time_out = 0
#self.logger.error('Trying to move %s from %s to submit'% (next_request.get_attribute('prepid'), current_r_approval))
while current_r_approval != 'submit' and time_out <= 10:
time_out += 1
#get it back from db to avoid _red issues
next_request = request(rdb.get(next_request.get_attribute('prepid')))
with locker.lock('{0}-wait-for-approval'.format( next_request.get_attribute('prepid') )):
next_request.approve()
request_saved = rdb.save(next_request.json())
if not request_saved:
raise self.ChainedRequestCannotFlowException(self.get_attribute('_id'),
'Could not save the new request %s while trying to move to submit approval' % (
next_request.get_attribute('prepid')))
current_r_approval = next_request.get_attribute('approval')
pass
return True
示例8: next_id
def next_id(self, pwg, campaign):
ccamp_db = database(self.ccamp_db_name)
creq_db = database(self.creq_db_name)
if not pwg:
self.logger.error('Physics working group provided is None.')
return None
if not campaign:
self.logger.error('Campaign id provided is None.')
return None
with locker.lock("{0}-{1}".format(pwg, campaign)):
if not ccamp_db.document_exists(campaign):
self.logger.error('Campaign id {0} does not exist.'.format(campaign))
return None
if (campaign, pwg) in self.serial_number_cache:
sn = self.serial_number_cache[(campaign, pwg)] + 1
else:
sn=1
serial_number_lookup = creq_db.raw_query('serial_number', {'group':True, 'key':[campaign, pwg]})
if serial_number_lookup:
sn = serial_number_lookup[0]['value']+1
## construct the new id
new_prepid = pwg + '-' + campaign + '-' + str(sn).zfill(5)
if sn==1:
self.logger.log('Beginning new prepid family: %s' % (new_prepid))
new_request = chained_request({'_id':new_prepid, 'prepid':new_prepid, 'pwg':pwg, 'member_of_campaign':campaign})
new_request.update_history({'action':'created'})
creq_db.save(new_request.json())
self.serial_number_cache[(campaign, pwg)] = sn
self.logger.log('New chain id: %s' % new_prepid, level='debug')
return new_prepid
示例9: flush
def flush(self,Nmin):
res=[]
with locker.lock('accumulating_notifcations'):
for key in self.cache.keys():
(subject,sender,addressee)=key
if self.cache[key]['N'] <= Nmin:
## flush only above a certain amount of messages
continue
destination = addressee.split(COMMASPACE)
text = self.cache[key]['Text']
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = addressee
msg['Date'] = formatdate(localtime=True)
new_msg_ID = make_msgid()
msg['Message-ID'] = new_msg_ID
msg['Subject'] = subject
## add a signature automatically
text += '\n\n'
text += 'McM Announcing service'
#self.logger.log('Sending a message from cache \n%s'% (text))
try:
msg.attach(MIMEText(text))
smtpObj = smtplib.SMTP()
smtpObj.connect()
smtpObj.sendmail(sender, destination, msg.as_string())
smtpObj.quit()
self.cache.pop(key)
res.append( subject )
except Exception as e:
print "Error: unable to send email", e.__class__
return res
示例10: get_user_role
def get_user_role(cls, username, email=None):
if not username:
return 'user'
with locker.lock(username):
cache_key = 'authenticator_user_role_' + username
cached_value = cls.__users_roles_cache.get(cache_key)
if cached_value is not None:
return cached_value
user_role = 'user'
if cls.__db.document_exists(username):
user = cls.__db.get(username)
if email and ('email' not in user or user['email'] != email):
user['email'] = email
cls.__db.update(user)
try:
user_role = user['role']
except Exception:
cls.logger.error('Error getting role for user "' + username + '". Will use default value "' + user_role + '"')
cls.__users_roles_cache.set(cache_key, user_role, timeout=cls.CACHE_TIMEOUT)
return user_role
示例11: GET
def GET(self, *args):
"""
Provides the injection command and does the injection.
"""
if not len(args):
return dumps({"results" : False, "message" : "no argument was passe"})
pid = args[0]
from tools.handlers import ChainRequestInjector, submit_pool
_q_lock = locker.thread_lock(pid)
if not locker.thread_acquire(pid, blocking=False):
return dumps({"prepid": pid, "results": False,
"message": "The request {0} request is being handled already".format(
pid)})
thread = ChainRequestInjector(prepid=pid, lock=locker.lock(pid), queue_lock=_q_lock,
check_approval=False)
if self.mode == 'show':
cherrypy.response.headers['Content-Type'] = 'text/plain'
return thread.make_command()
else:
submit_pool.add_task(thread.internal_run)
#thread.start()
return dumps({"results" : True,
"message" : "chain submission for %s will be forked unless same request is being handled already" % pid,
"prepid" : pid})
示例12: count_call
def count_call(self):
# counter for calls
method = request.method
with locker.lock("rest-call-counter"):
key = self.__class__.__name__ + method
try:
RESTResource.call_counters[key] += 1
except KeyError:
RESTResource.call_counters[key] = 1
示例13: get
def get(label):
with locker.lock(label):
cache_key = 'settings_' + label
cached_value = __cache.get(cache_key)
if cached_value is not None:
return cached_value
setting = __db.get(label)
__cache.set(cache_key, setting)
return setting
示例14: set
def set(label, setting):
with locker.lock(label):
result = __db.update(setting)
if result:
# Maybe it's a better idea to cache the setting immediately instead
# getting it from database?
new_value = __db.get(label)
cache_key = 'settings_' + label
__cache.set(cache_key, new_value)
return result
示例15: fill_id
def fill_id(self, pwg, db):
mccm_id = pwg
with locker.lock(mccm_id): # get date and number
t = mccm.get_meeting_date()
mccm_id += '-' + t.strftime("%Y%b%d") + '-' # date
final_mccm_id = mccm_id + '00001'
i = 2
while db.document_exists(final_mccm_id):
final_mccm_id = mccm_id + str(i).zfill(5)
i += 1
return final_mccm_id