本文整理汇总了Python中database.memcached_database.MemcachedDatabase.get_lock方法的典型用法代码示例。如果您正苦于以下问题:Python MemcachedDatabase.get_lock方法的具体用法?Python MemcachedDatabase.get_lock怎么用?Python MemcachedDatabase.get_lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database.memcached_database.MemcachedDatabase
的用法示例。
在下文中一共展示了MemcachedDatabase.get_lock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ObjectUpdater
# 需要导入模块: from database.memcached_database import MemcachedDatabase [as 别名]
# 或者: from database.memcached_database.MemcachedDatabase import get_lock [as 别名]
class ObjectUpdater(DatabaseHook):
'''This class checks objects in the world, and updates them
if required.
Responsibilities of this class:
* Killing and removing a robot from the world, if its
energy reached zero or it ran out of life.
* Removing a plant from the world if its water level
reached zero or it became too old.
* Maturing a plant if it reached a certain age.
* Updating a plant's water level during time.
'''
def __init__(self):
self._database = MemcachedDatabase()
self._configs = Configs()
def robot_got(self, robot_object, locked_for_update):
'''Checks and updates the specified robot's object.'''
if robot_object.get_energy() <= 0 or robot_object.get_life() <= 0:
if not locked_for_update:
# This would call this method again, and robot will be updated.
return self._database.get_robot(robot_object.get_id(), for_update=True)
robot_object.set_alive(False)
# Removing the robot from its location.
try:
square = self._database.get_square(robot_object.get_location(), for_update=True)
except LockAlreadyAquiredError:
# Trying one more time.
time.sleep(0.02)
square = self._database.get_square(robot_object.get_location(), for_update=True)
square.set_robot_id(None)
# XXX: Though it's a very dirty thing to do, we have to commit these changes, because
# later this robot will face an Authentication Exception, and our changes will be
# lost.
self._database.commit()
# Immediately, locking the robot object. It's not atomic, so there's a little chance
# that concurrency happens. But, it shouldn't be a problem, since the robot is already
# dead, and can't do anything anyway.
self._database.get_lock(robot_object.get_id())
return robot_object
def square_got(self, location, square_object, locked_for_update):
'''Checks and updates specified square object.'''
plant = square_object.get_plant()
if plant is None:
return square_object
# Time passed from the last time this plant updated.
last_update = time.time() - plant.get_last_update()
passed_cycles = math.floor(last_update / (self._configs.get_plant_cylce() / 1000))
if passed_cycles <= 0:
# No cycle passed, no need to be updated.
return square_object
if not locked_for_update:
# This will call this method again.
try:
return self._database.get_square(location, for_update=True)
except LockAlreadyAquiredError:
# Trying one more time.
time.sleep(0.02)
return self._database.get_square(location, for_update=True)
plant.set_age(plant.get_age() + passed_cycles)
plant.set_water_level(plant.get_water_level() - (passed_cycles * self._configs.get_plant_lose_water_in_cycle()))
if plant.get_age() > self._configs.get_plant_max_age() or plant.get_water_level() <= 0:
# Plant is dead! Removing it from the world.
square_object.set_plant(None)
plant.set_last_update(time.time())
return square_object