Skip to content

Commit b17742b

Browse files
committed
Generate unique identifiers
Uniqueness is guaranteed inside a single run
1 parent 60e3d6e commit b17742b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

unblob/identifiers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from threading import Lock
3+
4+
__last_id = 0
5+
__last_id_lock = Lock()
6+
7+
8+
def new_id():
9+
# NOTE, that uuid4 can not be used, as there are multiple processes at run time,
10+
# and as subprocesses inherit the random number state, so uuid4 would generate colliding ids
11+
# another option that would not work is to use uuid1, but we could generate the id
12+
# at the same time
13+
14+
global __last_id
15+
with __last_id_lock:
16+
__last_id += 1
17+
return f"{os.getpid()}:{__last_id}"

0 commit comments

Comments
 (0)