python - Is it time.time() a safe approach when creating content types on plone programatically? -
i have use _createobjectbytype
on plone. have argument id
of object. going safe, in scenario, create id based on time.time()
avoid collisions? can 2 requests have same timestamp shown time.time()
?
you'll safe doing this, in unlikely event 2 requests processed @ same time, in event of conflict zodb raise conflicterror , retry request.
responding discussion below:
on single computer defition both transactions must overlap (you got same result time.time() in each thread.) zodb mvcc, each thread sees consistent view of database when transaction began. when second thread commits, conflict error raised because write object has changed since beginning of transaction.
if have clients running on multiple computers need think possibility of clock drift between clients. transaction ids, zodb chooses whichever greater of either current timestamp or last transaction id + 1.
however, perhaps should consider not using timestamp id @ all, lead conflicts under heavy load, requests want create entries in same btree bucket. picking ids randomly eliminate of conflicts, lead inefficiently filled btrees. recommended approach each thread creates objects start @ random point in number space , create ids sequentially. if finds id has been used should randomly pick point in number space , start again there. believe zope.intid contains implementation of strategy.
Comments
Post a Comment