def _get_numbers_helper(keys, needed):
    results = []
for key in keys:
    seq = db.get(key)
start = seq.current or seq.start
end = seq.end
avail = end - start
consumed = needed
if avail <= needed:
    seq.active = False
consumed = avail
seq.current = start + consumed
seq.put()
results += list(range(start, start + consumed))
needed -= consumed
if needed == 0:
    return results
raise RuntimeError('Not enough sequence space to allocate %d numbers.' % needed
    )

def get_numbers(needed):
    query = gaetkSequence.all(keys_only=True).filter('active = ', True)
return db.run_in_transaction(_get_numbers_helper, query.fetch(5), needed)
Как реализовать «автоинкремент» в Google AppEngine