Tuesday, May 21, 2013

Re: Object Lookup after Save

On Tue, May 21, 2013 at 4:23 PM, Chris Conover <clcono@gmail.com> wrote:
> Hello,
>
> I'm having an issue looking up objects immediately after they are saved and
> am wondering if anyone has any advice on how to address the problem.
> Specifically, I'm saving an object in a view (nothing fancy, a simple
> save()) and then kicking off a Gearman task to do some operations on that
> saved object in the background. I pass the newly created object's PK as data
> to the Gearman worker which then does a simple Objects.objects.get(pk=PK).
> However, almost all of the time this lookup operation fails with an
> DoesNotExist exception. I believe the problem has to do with transactions.
> Namely, the read in the Gearman worker is happening before the write from
> the view is committed to the database. I've tried several things including
> refactoring the saving code to be wrapped in a
> @transaction.commit_on_success block, moving the worker submission to
> post_save and adding a long delay before the Gearman worker does the lookup.
> Nothing really seems to solve the problem completely. Even with a 60 second
> delay, the lookup fails with some frequency. Am I missing something here? Is
> there some Django query cache that I can clear? Or should I just rewrite all
> this to just to use a look-back perspective.
>
> The stack is Django 1.4 connecting to MySQL 5.5.27. Django is handling
> 200-1000 requests per second and the database is handling about double that.
>
> Thanks,
> Chris

from django import transaction

obj.save()
transaction.commit()
task.submit(obj.id)

You will also need to make sure that gearman is doing things correctly
as well. You haven't mentioned what database you are using, but if
gearman's DB connection is in a read repeated mode, you can do
whatever you like in django but you won't see new data in gearman
until gearman's current transaction is committed.

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+unsubscribe@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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate