Trying ZODB, background in Relational: mimic auto_increment?

Jean-Paul Calderone exarkun at divmod.com
Thu Aug 14 16:36:45 CEST 2008


On Thu, 14 Aug 2008 16:15:11 +0200, "Diez B. Roggisch" <deets at nospam.web.de> wrote:
>Jean-Paul Calderone wrote:
>
>> On Thu, 14 Aug 2008 05:22:35 -0700 (PDT), Phillip B Oldham
>> <phillip.oldham at gmail.com> wrote:
>>> [snip]
>>>
>>>How would one assign a unique ID to the root at that point?
>>
>> Here's one way
>>
>>     class Sequence(Persistence):
>>         def __init__(self):
>>             self.current = 0
>>
>>         def next(self):
>>             self.current += 1
>>             return self.current
>>
>>     ticketSequence = Sequence()
>>
>>     class Ticket(Persistence):
>>         def __init__(self):
>>             self.id = ticketSequence.next()
>
>Be aware that this isn't working concurrently. Depending on your
>application, this can be mitigated using a simple threading.Lock.
>

ZODB has transactions.  That's probably the best way to make this
code safe for concurrent use.  A threading.Lock would make threaded
use safe, but wouldn't give you multiprocess concurrency safety.

Jean-Paul



More information about the Python-list mailing list