[Zope3-Users] Threads and transactions

Alberto Degli Esposti ade8850 at gmail.com
Tue Dec 14 07:10:42 EST 2004


Hi,
 I have an application that needs to start some independent threads as
a consequence of a user request. For example, user submits a list of
files in a form, an utility uses a thread to do a checksum on each
file (very large files) then, when finished, sets the result as
annotation to an object. I think that each thread needs his own
transaction (or his own db connection) but the
db/connection/transaction API is not clear for me and I haven't found
the correct way to follow yet. I saw that I can subscribe the
IDataBaseOpenedEvent to get a db reference to initialize the utility,
but for some reasons this is not a good solution for my app. Can
someone help me please?

In the following, Checksum is a threading.Thread derived class that
calls the 'call_after' argument which is an UploadUtility.__checksum
instance:

class UploadUtility(object):

   implements(IUploadUtility)

   class __checksum(object):
       def __init__(self, utility, obj):
           self._obj=obj
           self._utility=utility

       def __call__(self, result):
           # called by checksum thread
           IAnnotations(self._obj)["raigrid.checksum"]=result
           self._utility.notifyEndChecksum(self._obj)

   def beginChecksum(self, obj):
       """
       start checksum thread
       """
       call_after=self.__checksum(self, obj)
       thrd=Checksum(IFileInfo(obj).path(), call_after)  ## __START
CHECKSUM__ ##
       thrd.start()

   def notifyEndChecksum(self, obj):
       """
       do something with checksum result
       """
       # [...]


Thanks 
#a


More information about the Zope3-users mailing list