[Zope3-dev] Re: Thread problems

Philipp von Weitershausen philipp at weitershausen.de
Mon Oct 11 17:17:20 EDT 2004


Peter Mayne wrote:
> I'm creating a thread using the Python threading module to perform a 
> long running task.
> 
> As soon as I attempt to modify something, I get:
> 
> Exception in thread Thread-3:
> Traceback (most recent call last):
>   File "/usr/local/lib/python2.3/threading.py", line 436, in __bootstrap
>     self.run()
>   File "/home/zope/zopex3/lib/python/imdb/browser/loader.py", line 47, 
> in run
>     self._loader()
>   File "/home/zope/zopex3/lib/python/imdb/browser/loader.py", line 192, 
> in loadActors_
>     parent = self.getSubfolder(ACTORS_FOLDER)
>   File "/home/zope/zopex3/lib/python/imdb/browser/loader.py", line 57, 
> in getSubfolder
>     self.context[name] = subfolder
> AttributeError: 'zope.thread.local' object has no attribute 'interaction'
> 
> The thread-local storage seems to be where zope.security stores its 
> stuff. How do I get my thread to inherit the right security?

Heh, well, I'd *really* like to tell you "Go read my book where this is 
all documented" now but it's not out yet :(. Let's see if I can make 
this story really short:

The Zope3 security system knows low-level componetns and high-level 
ones. They interact together but in a decoupled way.

On the low level you have security proxies wrapping all sorts of objects 
to protect them from unauthorized access. On the high level, you have 
interactions which are like transactions but for security. Basically, an 
interaction starts out with the participation of a user, for example 
through an HTTP request. Through the life time of an interaction, 
participants can come and go but usually you only have one like in the 
case of a request.

Now when a proxy needs to check whether some attribute may be accessed 
or written, it asks the current interaction whether one of the 
participants has the necessary privileges. The default setting is that 
there's always one interaction per thread (since zope.server starts a 
new thread for a new request). So, in your thread, you're dealing with 
proxied objects but you don't have an interaction in that thread.

Two solutions to your problem:

- only deal with the bare, non-proxied objects in the thread (if you 
don't care about security that much in this thread)

- start an interaction in the thread and use a participation that either 
has all privileges like SystemConfigurationParticipation (again, if you 
don't care about security) or use the participation(s) from the original 
thread that started out with a request so that the stuff that is 
executed in your thread runs under the same privileges as the original one.

The functions you need to use (newInteraction, endInteraction, 
getInteraction) are in zope.security.management. The interfaces in 
zope.security.interfaces should give you some clues. And in January you 
can buy my book *wink*.

Philipp



More information about the Zope3-dev mailing list