On Wed, May 02, 2012 at 12:49:32PM -0400, Claudiu Saftoiu wrote:
> I'm using a thread to do some server-side work. The thread will be
> asked by different requests to do the same thing at the same time, so
> I want the thread to do all the work once and return the data to the
> requests. The problem is that the requests each have their own
> transaction and the thread essentially has none.


> I can communicate between the two only using identifiers - not
> persistent objects - that way the thread can process data in a
> different transaction than the requests yet still return a meaningful
> reply.

Also correct: you should not share instances of persistent objects between

> However, the thread has to start a new transaction each time it
> processes something - which I know how to do:
>     while True:
>         #wait until asked to do something
>         import transaction
>         transaction.begin()
> However, the thread needs access to the root object in order to turn
> the identifiers gotten from the requests into persistent objects...
> how would I go about accessing the root object in such a circumstance?

You need to pass the database object to the thread, and the thread needs
to open a connection (connection = db.open()).  Then connection.root()
will give you the root object (or you could pass OIDs to the thread and
use connection.get(oid) to find the objects you need to work with).

Don't forget to commit or abort the transaction, and also don't forget
that you may need to implement some kind of retry logic if commit()
raises a ConflictError due to conflicting updates.

Marius Gedminas
main(k){float i,j,r,x,y=-16;while(puts(""),y++<15)for(x
=0;x++<84;putchar(" .:-;!/>)|&IH%*#"[k&15]))for(i=k=r=0;
/* Mandelbrot in ASCII. */

Attachment: signature.asc
Description: Digital signature

For more information about ZODB, see http://zodb.org/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org

Reply via email to