Updating a module level shared dictionary

Dave Angel davea at ieee.org
Wed Jun 16 07:29:59 EDT 2010


Vishal Rana wrote:
> Hi,
>
> A module level dictionary 'd' and is accessed by different threads/requests
> in a django web application. I need to update 'd' every minute with a new
> data and the process takes about 5 seconds.
>
> What could be best solution where I want the users to get either the old
> value or the new and nothing in between.
>
> I can think of a solution where a temp dictionary is constructed with a new
> data and assigned to 'd' but now sure how this works!
>
> Appreciate your ideas.
>
> Thanks
>
>   
The other answers I've seen so far are correct, as far as they go.  
Creating a new dictionary, and only binding it to the 'd' name after 
it's complete is the best you can do.

However, it'd be easy for one of those threads to abuse the situation.  
If they keep their own reference to data from d, and go back to d for 
more information, then they could be using stale and new data, 
respectively, in the same thread.

So in addition to making sure you build the entire dictionary before 
assigning it to d, you have to make sure all users of d make their own 
name for it, only referencing the global 'd' once per thread.  There are 
other ways, but this is the simplest to describe.  Realize that if you 
have a long-running thread (say 15 minutes), then it'd continue using 
the version of d that existed when it started.  This shouldn't be a 
problem for a typical web app, however, because the user could get very 
impatient waiting for his update.

DaveA




More information about the Python-list mailing list