On Sat, May 11, 2013 at 1:04 AM,  <testbackupa...@gmail.com> wrote:
> Hi,
>
> I'm pretty new to Django and am having a problem with a race conditions
> while modifying my session data. I'm using the standard session backend in
> Django 1.4.1,, backed by Mysql.
>
> I have view A, which can take a long time to process, and view B, which is
> usually faster. I store multiple data fields, fieldA and fieldB, in the
> session dictionary. View A modifies fieldA, and view B modifies fieldB.
>
> Sometimes, view A starts up with a copy of the session data as it exists at
> t1. Then, while it's processing, view B starts up. It completes quickly,
> modifying fieldB in the session data at time t2. Then view A finally
> completes, modifying its field in the session data at time t3. But what I
> wind up with is fieldA at time t3 and fieldB at time t1. In other words,
> when view A completes its write, it's storing the stale version of fieldB
> that it read when it started. What I want in that situation is t3's version
> of fieldA and t2's version of fieldB.
>
> My questions: (1) is there a way to check if your copy of the session
> dictionary is stale? And if so, is there a way to do it atomically? (2) Is
> there a way to selectively write just part of the session data, instead of
> the whole thing? (3)  Or, is there a better solution besides either of these
> ideas?
>
> The only thing I can think of using RAM memory to keep track of a last_write
> timestamp for each session -- but that's not a scalable solution. Is there a
> better way?
>
> Thanks in advance,
>
> Spork
>

There are only two hard problems in computer science, cache
invalidation, naming things and off by one errors.

Detecting 'stale' session data is very tricky, I would not bother.
Mostly this can be avoided by not using session to store this state
data, eg by using a specific model to store each type of data that you
are calculating.

Potentially easier in this specific case would be to reload the
session from the database prior to updating it and saving it. This of
course would throw away any other changes made to unrelated other
parts of the session (which of course you would not get if you were
creating/editing/saving specific objects rather than an arbitrary blob
of session objects.)

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to