[Python-Dev] Weird use of hash() -- will this work?
Eric S. Raymond
Thu, 18 Jan 2001 02:23:21 -0500
So I'm writing a module to that needs to generate unique cookies. The
module will run inside one of two environments: (1) a trivial test wrapper,
not threaded, and (2) a lomg-running multithreaded server.
Because Python garbage-collects, hash() of a just-created object isn't
good enough. Because we may be threading, millisecond time isn't
good enough. Because we may *not* be threading, thread ID isn't good
On the other hand, I'm on Linux getting millisecond time resolution.
And it's not hard to notice that an object hash is a memory address.
So, how about `time.time()` + hex(hash())?
It looks to me like this will remain unique forever, because another thread
would have to create an object at the same memory address during the same
millisecond to collide.
Furthermore, it looks to me like this hack might be portable to any OS
with a clock tick shorter than its timeslice.
<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>
Good intentions will always be pleaded for every assumption of
authority. It is hardly too strong to say that the Constitution was
made to guard the people against the dangers of good intentions. There
are men in all ages who mean to govern well, but they mean to
govern. They promise to be good masters, but they mean to be masters.
-- Daniel Webster