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

Jonathan dev101 at magma.ca
Mon Jun 27 10:19:22 EDT 2005


I expect that your 'global BUSY_STATE' statement is only creating a global 
variable within the context of a 'program execution'.

In general terms, a global variable is only accessible to routines within a 
single running program.  Two programs, running simultaneously, will each 
have their own versions of the global variable (even if the global variable 
has the same name). If this were not the case then any two programs running 
at the same time (on the same computer), with matching variable names, would 
change the contents of the variables in both programs -> resulting in 
unintended/uncontrollable program behaviour!

If you have two copies of your external method running (as a result of 
overlapping wget calls) each external method will run as a stand-alone 
program and will not know about any other external methods that are running.

A possible solution:  create a property field on the folder where the 
external methods are stored.  Have your external method update this property 
field when the external method starts and again when it exits.  This way you 
can test whether or not the external method is currently in operation.  You 
could store this property field on a temp_folder for faster performance.

hth

Jonathan



----- Original Message ----- 
From: "Max M" <maxm at mxm.dk>
To: <zope at zope.org>
Sent: Monday, June 27, 2005 9:53 AM
Subject: [Zope] Sharing global data between threads / locking a method


>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
>
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
> http://mail.zope.org/mailman/listinfo/zope-dev )
> 




More information about the Zope mailing list