[Zope3-dev] Re: using utilities/sites from threads in zope3
Tim Stebbing
tjstebbing at gmail.com
Mon Jul 2 21:04:11 EDT 2007
Thanks Tres, I've ended up with a threadHelper.py module that seems to
be working well, I'll post it as its small and might be useful for
people googling in the future.
"""Helper utilities for working with threads."""
from ZODB.interfaces import IDatabase
from zope.app import zapi
from zope.app.traversing.api import traverseName
from zope.app.component.hooks import setSite
from zope.app.component.interfaces import ISite
from transaction import manager
def setupWorkerThread(path):
"""sets up the regular contexts for your worker thread, takes a path which
can be found with zope.app.traversing.api.getPath(obj), the site context of
your thread will be configured based on this path and the object at the
path returned."""
db = zapi.getUtility(IDatabase) #XXX assumes we have only one db
conn = db.open()
root = conn.root()
manager.begin()
ctx = root['Application']
for seg in path.split('/')[1:]:
ctx = traverseName(ctx, seg.encode("ASCII"))
if ISite.providedBy(ctx):
setSite(ISite(ctx))
return ctx
def closeWorkerThread():
manager.commit()
On 7/3/07, Jim Fulton <jim at zope.com> wrote:
> What Tres said. :)
>
> Jim
>
> On Jun 29, 2007, at 8:54 AM, Tres Seaver wrote:
>
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > Tim Stebbing wrote:
> >> G'day,
> >>
> >> I've recently ran into a bit of a problem attempting to use
> >> threads in
> >> zope 3.2.1, hooks.getSite() returns None, and any attempt to get a
> >> utility results in a component lookup error within my threads.
> >>
> >> I've attempted to pass a site into a thread and set it with
> >> hooks.setSite() but this is apparently not the way to go, I get the
> >> feeling that a thread that needs access to things form the database
> >> probably needs its own connection, context etc.
> >
> > Your worker thread almost certainly needs its own database
> > connection(s): applicaiton code typically relies on the isolation
> > provided by connection-per-thread, which means that it is not
> > "safe" in
> > general to share persistent objects between threads.
> >
> > Assuming that you do grab a connection and get its root object, you
> > should then be able to traverse to your friendly local site manager,
> > calling 'setSite' at *each* parent site manager you pass along the
> > way.
> >
> >> I'm aware of ITask's but unsure if developers are supposed to use
> >> them
> >> for their own purposes? Do these handle setting up of contexts like
> >> db, site, global utils etc? I've googled around a fair bit and been
> >> over all the z3 doco, it would be great if there was a simple
> >> howto on
> >> setting up a thread that would have the usual access to various
> >> components.
> >
> >
> > Tres.
> > - --
> > ===================================================================
> > Tres Seaver +1 540-429-0999 tseaver at palladion.com
> > Palladion Software "Excellence by Design" http://palladion.com
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v1.4.6 (GNU/Linux)
> > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> >
> > iD8DBQFGhQEG+gerLs4ltQ4RAh6EAJ9DzBW20NPz4tAE3BydJ789Vokx0wCg2qMp
> > yBB8XemrjD7dXPwwwz4y9Yo=
> > =+iwh
> > -----END PGP SIGNATURE-----
> > _______________________________________________
> > Zope3-dev mailing list
> > Zope3-dev at zope.org
> > Unsub: http://mail.zope.org/mailman/options/zope3-dev/jim%40zope.com
> >
>
> --
> Jim Fulton mailto:jim at zope.com Python Powered!
> CTO (540) 361-1714 http://www.python.org
> Zope Corporation http://www.zope.com http://www.zope.org
>
>
>
>
--
Timothy J Stebbing
More information about the Zope3-dev
mailing list