Hi Kent, Kent Sin wrote:
I want to develope a unique sequence id generator product, I have the following code
def newid(self, increment=1): """ return new id """ self.counter = self.counter+increment return self.counter
I am not quite sure if this works when more than one process asking for a newid concurrently.
Do you need unique sequential ids, or will just unique ids do? The problem with sequential ids is that you need to store the next id in the sequence somewhere, and access to and incrementing this value becomes a performance bottleneck for your server. Also, if you store the sequence value in the ZODB, you will be creating an undo record each time the id is incremented, and this is an additional performance and storage overhead. If you just want ids that are unique to a folder, try the following algorithm (taken originally from the Discussion object code in the PTK). The variable "self" is the one passed to the constructor method of a particular Python class, and represents the folder you want to put the new object into. I'm assuming the object is a "FooBar Item". The "foobar-%06d" bit generates a unique id for the object that looks like "foobar-290172837", based on the current time. id = int(DateTime().timeTime()) while hasattr(self, str(id)): id = id +1 id = 'foobar-%09d' % id The advantage of this approach is that there is very little contention in most cases.
Is that zodb automatically serial all transaction?
For the purposes of generating ids, yes. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
On 2 July, 2000 Steve Alexander wrote:
If you just want ids that are unique to a folder, try the following algorithm (taken originally from the Discussion object code in the PTK). The variable "self" is the one passed to the constructor method of a particular Python class, and represents the folder you want to put the new object into. I'm assuming the object is a "FooBar Item". The "foobar-%06d" bit generates a unique id for the object that looks like "foobar-290172837", based on the current time.
id = int(DateTime().timeTime()) while hasattr(self, str(id)): id = id +1 id = 'foobar-%09d' % id
The advantage of this approach is that there is very little contention in most cases.
I just looked back over some code where I used this algorithm, and I realised that it doesn't actually work :-( The identifiers that are searched for do not reflect the identifiers that are produced. The original code from the PTK does work. Here it is, adapted very slightly: # Find an unused id in location id = int(DateTime().timeTime()) while hasattr(self, `id`): id = id + 1 return id Here's my own, now fixed, code. Note the addition of a "max_tries" variable. if id == 'auto' or id == None: n = int(DateTime().timeTime()) max_tries = 10 fk = lambda key: 'foobar-%d' % key # function to format key id = fk(n) while max_tries and hasattr(self, str(id)): n = n + 1 max_tries = max_tries-1 id = fk(n) Sorry for posting such rubbish the first time around :-/ -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
A different approach: For each entry I just use the current date/time including a call: time.sleep(0.001) This way I am sure each entry will have a unique id. It is only slightly slower (0.001 second for each calc). However, it requires serial processing of entries and a stable time (only server-side processing). Therefore very usable for processing batch-entries as I do. Gijs Reulen
-----Oorspronkelijk bericht----- Van: alexande@cat-box.net [mailto:alexande@cat-box.net]Namens Steve Alexander Verzonden: vrijdag 28 juli 2000 19:20 Aan: zope@zope.org; Sin Hang Kin Onderwerp: [Zope] Re: Unique id product
On 2 July, 2000 Steve Alexander wrote:
If you just want ids that are unique to a folder, try the following algorithm (taken originally from the Discussion object code in the PTK). The variable "self" is the one passed to the constructor method of a particular Python class, and represents the folder you want to put the new object into. I'm assuming the object is a "FooBar Item". The "foobar-%06d" bit generates a unique id for the object that looks like "foobar-290172837", based on the current time.
id = int(DateTime().timeTime()) while hasattr(self, str(id)): id = id +1 id = 'foobar-%09d' % id
The advantage of this approach is that there is very little contention in most cases.
I just looked back over some code where I used this algorithm, and I realised that it doesn't actually work :-( The identifiers that are searched for do not reflect the identifiers that are produced.
The original code from the PTK does work. Here it is, adapted very slightly:
# Find an unused id in location id = int(DateTime().timeTime()) while hasattr(self, `id`): id = id + 1 return id
Here's my own, now fixed, code. Note the addition of a "max_tries" variable.
if id == 'auto' or id == None: n = int(DateTime().timeTime()) max_tries = 10 fk = lambda key: 'foobar-%d' % key # function to format key id = fk(n) while max_tries and hasattr(self, str(id)): n = n + 1 max_tries = max_tries-1 id = fk(n)
Sorry for posting such rubbish the first time around :-/
-- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (2)
-
Gijs Reulen -
Steve Alexander