Re: Querysets returns wrong result when using threading.

2010-01-17 Thread Kieran Brownlees
Fantastic, thank you :)

On Jan 16, 9:02 pm, Tomasz Zieliński
 wrote:
> On 16 Sty, 06:15, Kieran Brownlees  wrote:
>
> > Firstly thank you, secondly, how to get around it? I assume I need to
> > force a commit for the transaction my thread is using, but querysets
> > don't appear to have a documented method to do that.
>
> Yes, there is separate module for that:
>
> from django.db import transaction
> transaction.commit()
>
> --
> Tomasz Zielinskihttp://pyconsultant.eu
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Querysets returns wrong result when using threading.

2010-01-15 Thread Kieran Brownlees
Firstly thank you, secondly, how to get around it? I assume I need to
force a commit for the transaction my thread is using, but querysets
don't appear to have a documented method to do that.

Thank you

On Jan 16, 1:52 am, Tomasz Zieliński
 wrote:
> On 15 Sty, 05:33, James Bennett  wrote:
>
> > On Thu, Jan 14, 2010 at 10:26 PM, Kieran Brownlees  
> > wrote:
> > > Basic example of format:
> > > Main Thread: print objects.all()
> > > Spawned Thread: print objects.all() -- same as main thread
> > > Main Thread: objects.create(newObj)
> > > Main Thread: print.objects.all() -- correct queryset, original + new
> > > Spawned Thread: print objects.all() -- only contains the original
> > > objects, not the new one?
>
> > This would be the expected result with the default transaction
> > isolation of most databases, and has nothing to do with threading.
>
> To be more explicit - Python DB API spec states that connection
> should, by default, have autocommit turned on - which means that there
> is always an ongoing transaction at DB level.
>
> Now, for MySQL backend, which I'm most familiar with,
> default isolation level is REPEATABLE READ, which means
> that first access to given table freezes its state for the duration
> of current transaction.
>
> And because Django default behaviour is @autocommit,
> i.e. it doesn't issue any COMMIT/ROLLBACK statements
> for fetches, only for writes, all subsequent queries results
> in the same queryset (in terms of elements, not object id).
>
> --
> Tomasz Zielinskihttp://pyconsultant.eu
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Querysets returns wrong result when using threading.

2010-01-15 Thread Tomasz Zieliński


On 15 Sty, 05:33, James Bennett  wrote:
> On Thu, Jan 14, 2010 at 10:26 PM, Kieran Brownlees  
> wrote:
> > Basic example of format:
> > Main Thread: print objects.all()
> > Spawned Thread: print objects.all() -- same as main thread
> > Main Thread: objects.create(newObj)
> > Main Thread: print.objects.all() -- correct queryset, original + new
> > Spawned Thread: print objects.all() -- only contains the original
> > objects, not the new one?
>
> This would be the expected result with the default transaction
> isolation of most databases, and has nothing to do with threading.
>

To be more explicit - Python DB API spec states that connection
should, by default, have autocommit turned on - which means that there
is always an ongoing transaction at DB level.

Now, for MySQL backend, which I'm most familiar with,
default isolation level is REPEATABLE READ, which means
that first access to given table freezes its state for the duration
of current transaction.

And because Django default behaviour is @autocommit,
i.e. it doesn't issue any COMMIT/ROLLBACK statements
for fetches, only for writes, all subsequent queries results
in the same queryset (in terms of elements, not object id).

--
Tomasz Zielinski
http://pyconsultant.eu
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Querysets returns wrong result when using threading.

2010-01-14 Thread James Bennett
On Thu, Jan 14, 2010 at 10:26 PM, Kieran Brownlees  wrote:
> Basic example of format:
> Main Thread: print objects.all()
> Spawned Thread: print objects.all() -- same as main thread
> Main Thread: objects.create(newObj)
> Main Thread: print.objects.all() -- correct queryset, original + new
> Spawned Thread: print objects.all() -- only contains the original
> objects, not the new one?

This would be the expected result with the default transaction
isolation of most databases, and has nothing to do with threading.


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Querysets returns wrong result when using threading.

2010-01-14 Thread Kieran Brownlees
Hello All,

Using Django 1.1.1. Today I found some very strange behaviour when
trying to use theading, from what I understand it is different from
the notes on trac of querysets not being thread safe.

Basic issue trying to execute queries while in a thread always returns
an original query. Probably best explained with an example (bit rushed
by time at the end of the day! Sorry for the non generic example.):

http://pastebin.com/m63211552

Basic example of format:
Main Thread: print objects.all()
Spawned Thread: print objects.all() -- same as main thread
Main Thread: objects.create(newObj)
Main Thread: print.objects.all() -- correct queryset, original + new
Spawned Thread: print objects.all() -- only contains the original
objects, not the new one?

Full output (also shown in pastebin)
test: []
In thread
threadTest: []
Thread waiting
Creating new db entry
test: [,
]
threadTest: []

Thank you,
Kieran


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