3 # © Copyright 2021-2022, Scott Gasch
6 A helper class for generating thread safe monotonically increasing
13 # This module is commonly used by others in here and should avoid
14 # taking any unnecessary dependencies back on them.
16 logger = logging.getLogger(__name__)
20 def get(name: str, *, start=0) -> int:
22 Returns a thread-safe, monotonically increasing id suitable for use
23 as a globally unique identifier.
26 name: the sequence identifier name.
27 start: the starting id (i.e. the first id that should be returned)
30 An integer id such that within one sequence identifier name the
31 id returned is unique and is the maximum id ever returned.
33 >>> import id_generator
34 >>> id_generator.get('student_id')
36 >>> id_generator.get('student_id')
38 >>> id_generator.get('employee_id', start=10000)
40 >>> id_generator.get('employee_id', start=10000)
43 if name not in generators:
44 generators[name] = itertools.count(start, 1)
45 x = next(generators[name])
46 logger.debug("Generated next id %d in sequence %s", x, name)
50 if __name__ == '__main__':