[Zope] Re: Sharing global data between threads / locking a method

Max M maxm at mxm.dk
Tue Jun 28 05:07:42 EDT 2005


Dieter Maurer wrote:
> Max M wrote at 2005-6-27 15:53 +0200:
> 
>>...
>>So in a external method/module I have a function like this:
>>
>>BUSY_STATE = 0
>>def sync_in_progress(busy=None):
>>    global BUSY_STATE
>>    if busy is None:
>>        return BUSY_STATE
>>    else:
>>        BUSY_STATE = busy
> 
> 
> Note that this is likely to fail.
> 
> The module containing an External Method is maintained in the
> ZODB cache. As a consequence, each worker gets its own copy
> and you cannot synchronize via global variables of such modules.
> 
> Use a true Python module (note that Zope does not import
> the source file of an External Method; therefore, it is
> not inside a module in the Python sense) when you need
> synchronization via global module level variables.

I wrote a small tool and ended up with this:

BUSY_STATE = 0

def sync_in_progress(busy=None):
     global BUSY_STATE
     print '----------------'
     print 'BUSY_STATE:', BUSY_STATE, 'busy:', busy
     print ''
     if not busy is None:
         BUSY_STATE = busy
     return BUSY_STATE

class Syncer(UniqueObject, PropertyManager,
                        SimpleItem.SimpleItem, ActionProviderBase):

     def redirect(self, url):
         self.REQUEST.RESPONSE.redirect(url)

     def reset(self):
         "reset"
         return repr(sync_in_progress(0))

     def sync(self):
         "Syncs"
         print '##################'
         print 'sync start'
         if not sync_in_progress():
             sync_in_progress(1)
             self.redirect('%s/sync_action' % self.absolute_url())
         else:
             return 'SYNC: in progress'

     def sync_action(self):
         "sync_action"
         # do stuff
         sync_in_progress(1)
         self.sync_calendar()
         self.sync_email()
         sync_in_progress(0)
         return 'SYNC: done'

It doesn't seem to work without the redirect. Which is the reason for 
having both a sync and a sync_action method.

-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Zope mailing list