[Zope3-dev] Re: Thread problems

Philipp von Weitershausen philipp at weitershausen.de
Tue Oct 12 03:04:35 EDT 2004


Peter Mayne wrote:
> Philipp von Weitershausen wrote:
> 
>> 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?
>>
>>
>> - 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*.
> 
> 
> I figured all of that out from Stephan's book and looking at the source. 
> I was hoping you could fill in the final bit and tell me *how* to use 
> those functions.

That's what interfaces are for :)

> I tried the obvious thing of passing the result of getInteraction() to 
> the thread, and doing a newInteraction() as the very first thing when 
> the thread starts, but I get:
> 
> 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 48, 
> in run
>     self._loader()
>   File "/home/zope/zopex3/lib/python/imdb/browser/loader.py", line 192, 
> in loadActors_
>     newInteraction(self._interaction)

You obviously didn't read the interfaces nor my explanation careful 
enough. An interaction is constructed out of participations ("principals 
participate together in an interaction"). You should be doing the following:

# in the request-invoked thread:
 >>> participiations = list(getInteraction().participations)

# in your custom thread (assuming that you have access to
# `participations`)
 >>> newInteraction(*participations)

Philipp



More information about the Zope3-dev mailing list