I want to write an object that, upon instantiation, generates a new ID for each instance. This ID however must be
- Generated in a thread and process safe manner
- Unique even across processes (spawned with multiprocessing)
A few non-concerns:
- This particular object creation is not performance critical so the synchronization overhead posed by this is acceptable.
- IDs must not be serial, though a clean solution usually comes with that.
- We are ignorant enough to not care about python 2 at all.
There are already some solutions which work only in one process, the most elegant being the use of an itertools.count()
object. Using id()
is no option as it is not guaranteed to be unique. The ideal solution would probably to be a similar object to itertools.count()
which holds some static global value across processes.
Related discussion on our project: https://github.com/coala-analyzer/coala/issues/981