From: MRAB on
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.
>
Making changes to a structure such as a dict isn't threadsafe, but
binding to a name is, so making a new dict, or copying the current dict
and changing the copy, and then binding, should be OK.
From: Ian Kelly on
On Tue, Jun 15, 2010 at 7:04 PM, Vishal Rana <ranavishal(a)gmail.com> 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

Constructing a new dictionary out-of-place and then assigning it to
the proper name sounds perfectly reasonable to me. Since the
assignment operation is atomic, you shouldn't even need to guard it
with a mutex.

Cheers,
Ian
From: Dave Angel on
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