[Zope] Which method to generate IDs for objects?

Thomas Guettler Thomas Guettler <thomas@thomas-guettler.de>
Fri, 18 Jan 2002 23:45:52 +0100


On Thu, Jan 17, 2002 at 11:30:46PM +0100, Dieter Maurer wrote:
> Thomas Guettler writes:
>  > On Wed, Jan 16, 2002 at 12:21:45PM -0000, Tim Hicks wrote:
>  > > I don't know if this is very efficient/elegant/etc*, but I use it to get a
>  > > unique id within a folder...
>  > > 
>  > > -------------
>  > > intid = 1
>  > > while 1:
>  > >    if hasattr(photo_folder, str(intid)):
>  > >      intid = intid + 1
>  > >    else:
>  > 
>  > 
>  > How thread-safe is this? I Java I would write a syncronise block
>  > around i=i+1, because of the following example:
> It is not thread safe and therefore the later "_setObject"
> may raise a BadRequest exception due to duplicate ids.
> 
> It is therefore better, to use the "_setObject" directly:
> 
>    while 1:
>      try: self._setObject(str(intid),o); break;
>      except BadRequest: intid= intid+1

Sorry, I don't understand this. Ain't there a easier way to get
something like a syncronized block in java?

BTW, I found something in lib/python/ZODB/tests/ConflictResolution.py:
The PCounter class. I think it is a persistent thread safe counter.

 thomas

-- 
Thomas Guettler <guettli@thomas-guettler.de>
http://www.thomas-guettler.de