AW: [Zope] Threading in Zope

Etienne Labuschagne elabuschagne@gmsonline.co.za
Thu, 17 Jul 2003 19:15:08 +0200


Florian,

Your new thread, does it need to change any Zope objects, or will it=20
process the PDF file and exit without changing any Zope objects?

If you are going to change Zope objects, you will have to create a new=20
connection into the ZODB, because passing Zope objects to new threads is A=
=20
VERY BAD IDEA.

If your threads do not change any Zope objects, it should be no problem,=20
i.e. starting a thread and only passing it the path where the PDF will be=20
created should be fine.  In general, passing any immutable object should be=
=20
fine (no, not tuples containing mutables!).

Passing Zope objects, and changing stuff on those objects . . .  well,=20
prepare to start gnawing off you own leg or something :)

The way I use Zope objects thread safely (please Chris, Dieter, or anyone=20
else correct me if I'm wrong!) goes something like this:

def myNewThreadMethod(pathToZopeObject):
   connection =3D Globals.DB.open(force=3D1) #you have a connection into the=
 DB=20
(remember to import Globals)
   app =3D connection.root()['Application'] #now we have the Zope root=
 object
   myObject =3D app.unrestrictedTraverse(pathToZopeObject) #we go and fetch=
=20
the object

   # maybe get the user object and log in as user here (see other=20
responses) if you want to

   myObject.doAllKindsOfStuff() #do whatever you want to
   get_transaction().commit() #or get_transaction().abort() if you don't=20
want the changes to stick (very important)
   app._p_jar.close()  #close the connection into the DB


def methodThatStartsNewThread(self, REQUEST, whateverElse):
   myObjectThatIWannaUseInAThread =3D self.howeverIGetToIt() #you now have a=
=20
Zope object - you cannot pass this to another thread!
   pathToMyObject =3D myObjectThatIWannaUseInAThread.getPhysicalPath() #get=
=20
the physical path relative to the root

   thread.start_new(myNewThread, (pathToMyObject)) #fire up your method,=20
passing it the path to the object

This may have Chris, Dieter and the likes gasping with shock and horror at=
=20
#1 either the wrongness (I'm sure this isn't too wrong), or #2 the=20
inelegance of it, but so far it works for me.

Understand that you cannot get your hands on a Zope object in one thread=20
and pass it to another thread to be changed.  This will lock up Zope and in=
=20
general make your whole life miserable.  You may get away with passing the=
=20
object for read only type access, but I don't bargain on it and don't do it=
=20
(gets glazed look, remembering loooong nights of suffering)

I hope that either this helps, or at least scares you off of doing it half=
=20
heartedly and getting burnt!

Regards and good luck
Etienne


At 02:39 PM 17/7/2003 +0200, Florian Reiser wrote:
>No I'm not. Perhaps I should explain you my problem.
>
>I have a zope object which compiles a huge pdf file for about 5 minutes
>or so.
>At some point an intermediate ISA server (M$ ;)) times out and sends an
>error page
>to the browser. Due to the error page the browser ignores the redirect
>header in the response.
>The redirect header should point the browser to a waiting page, which
>checks every 10 seconds,
>if the pdf file is generated yet. However IE sets the URL of the browser
>to the one received
>in the redirect header, but does not reload the window.
>
>Now I want to compute the pdf-file in a separate thread, so the
>originating one can return and show the
>redirection page before the ISA server times out.
>
>If you have another solution for me, how I can cause Zope the end the
>response before the method returns,
>I would be glad to hear it.
>
>Regards
>Florian Reiser
>
>-----Urspr=FCngliche Nachricht-----
>Von: Chris Withers [mailto:chrisw@nipltd.com]
>Gesendet: Donnerstag, 17. Juli 2003 14:29
>An: florian.reiser@ra-bc.de
>Cc: 'Zope-allgemein'
>Betreff: Re: [Zope] Threading in Zope
>
>
>Florian Reiser wrote:
> > Hi folks,
> >
> > When I start a thread in zope, which shall execute a zope object, then
>
> > the user falls back to the 'Anonymous User'.
> >
> > How can I preserve the current user for the thread?
> > I need the user, because the script assembles it's output according to
>
> > the users rights.
>
>You need seriously deep Zen to do this kind of programming... Are you
>_really_ sure you want to? ;-)
>
>cheers,
>
>Chris
>
>
>
>_______________________________________________
>Zope maillist  -  Zope@zope.org
>http://mail.zope.org/mailman/listinfo/zope
>**   No cross posts or HTML encoding!  **
>(Related lists -
>  http://mail.zope.org/mailman/listinfo/zope-announce
>  http://mail.zope.org/mailman/listinfo/zope-dev )