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

Max M maxm at mxm.dk
Mon Jun 27 09:53:38 EDT 2005


I have a synkronisation script that I run every 10 minutes via wget from 
a cron job.

Sometimes the script runs longer than 10 minutes.

In that case I would like to return a page to wget, but not run the 
actual script.

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

The idea is that BUSY_STATE is a global value shared between all 
threads, and if the sync_test() function below is called while it is 
allready running in another thread, it will just return 'Sync allready 
in progress'.

I then wrap the actual sync code like this:

def long_test_function(): # just to kill time
     t = Timer('Loop time')
     for i in xrange(10**7):
         d = 7*8
         e = 7*8
     print t.time()
     return d


def sync_test(self):
     if not sync_in_progress():
         sync_in_progress(1)
         long_test_function() # placeholder for the real sync code
         sync_in_progress(0)
         return 'Sync done'
     else:
         return 'Sync allready in progress'

But it seems that the new method merely waits until the first one has 
completed.

So it allways returns 'Sync done' and calls long_test_function()

What am I misunderstanding here? Isn't it the right way to share global 
data? Or is there some kind of locking going on under my nose that I am 
to blind to see.

-- 

hilsen/regards Max M, Denmark

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



More information about the Zope mailing list