3 # © Copyright 2021-2022, Scott Gasch
6 A helper class for generating thread safe monotonically increasing
11 This code is thread safe but not process safe; for use only
12 within one python process.
18 # This module is commonly used by others in here and should avoid
19 # taking any unnecessary dependencies back on them.
21 logger = logging.getLogger(__name__)
25 def get(name: str, *, start=0) -> int:
27 Returns a thread-safe, monotonically increasing id suitable for use
28 as a globally unique identifier.
31 name: the sequence identifier name.
32 start: the starting id (i.e. the first id that should be returned)
35 An integer id such that within one sequence identifier name the
36 id returned is unique and is the maximum id ever returned.
38 >>> import id_generator
39 >>> id_generator.get('student_id')
41 >>> id_generator.get('student_id')
43 >>> id_generator.get('employee_id', start=10000)
45 >>> id_generator.get('employee_id', start=10000)
48 if name not in generators:
49 generators[name] = itertools.count(start, 1)
50 x = next(generators[name])
51 logger.debug("Generated next id %d in sequence %s", x, name)
55 if __name__ == "__main__":