Model caching per python session

12 views
Skip to first unread message

Julian Hodgson

unread,
Aug 22, 2011, 12:16:24 PM8/22/11
to django-users
Hi there,

I'm running a production linux django server using wsgi, and have found the following issue. Django version (1, 2, 5, 'final', 0).

If I open a python shell I get:

>>> from passion.cg.models import *
>>> print Sequence.objects.all()
[<Sequence: DA>, <Sequence: DB>, <Sequence: DC>, <Sequence: DD>]


But if I go into the admin and delete sequence DD, leaving the python session running, then I still get

>>> print Sequence.objects.all()
[<Sequence: DA>, <Sequence: DB>, <Sequence: DC>, <Sequence: DD>]

so the Sequence table doesn't appear to be updated as far as the model is concerned.

It's pretty fundamental that this can be resolved since many different users will be using the database at the same time, and it should be possible for each user to see the latest state of the DB.

Any suggestions welcomed.

Cheers,

Julian




Daniel Roseman

unread,
Aug 22, 2011, 3:30:06 PM8/22/11
to django...@googlegroups.com
This isn't anything to do with caching. It's a result of the fact that the shell session is running within a single transaction, and therefore doesn't see changes from outside that. If you quit the shell and restart it, you'll be able to see the change.

This isn't a problem in production, because transactions in views are tied to the request/response cycle, which is short-lived.
--
DR.
 

Julian Hodgson

unread,
Aug 22, 2011, 7:29:46 PM8/22/11
to django...@googlegroups.com

Ok, that makes sense.

The thing is, we've written some python plugins for Softimage to read and write to the db that get loaded and stay in memory.   When the plugins are loaded, the django models are imported.  So during the lifetime of the host application, the python session is the same.

So I really need a way of closing the transaction or flushing the sql in django,  without starting a new python session. 

Any ideas warmly received.

Julian.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/qX11CSPon3MJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Julian Hodgson

unread,
Aug 23, 2011, 10:15:14 AM8/23/11
to django...@googlegroups.com
FYI: this does the trick:

from django.db import connection
connection.close()

Tom Evans

unread,
Aug 23, 2011, 11:11:51 AM8/23/11
to django...@googlegroups.com
On Tue, Aug 23, 2011 at 3:15 PM, Julian Hodgson
<julian.o...@googlemail.com> wrote:
> FYI: this does the trick:
>
> from django.db import connection
> connection.close()
>

This should work better:

from django.db import transaction

transaction.enter_transaction_management()
transaction.managed(True)
# do some work
transaction.commit()
# do some more work
transaction.commit()

transaction.leave_transaction_management()
transaction.managed(False)
# exit

As documented here:

https://docs.djangoproject.com/en/1.3/topics/db/transactions/

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages