def get_id():
"""Reserve a globally unique ID.
The system will create a random number between MIN_ID and MAX_ID. It then
attempts to create a record in datastore reserving that ID. If the attempt
succeeds, the ID is handed out. If it fails, it tries again.
"""
while True:
try:
candidate = random.randint(MIN_ID, MAX_ID)
# _check_and_create_record will create the record without using a
# transaction. Then _reserve_candidate will flip the reserved flag within
# a transaction. Unfortunately, it doesn't appear that transactional
# gurantees don't extend to two threads creating the same entity at the
# same time.
if _reserve_candidate(candidate):
return candidate
except TransactionFailedError:
pass
评论列表
文章目录