#34091: Invalid SQL: FROM clauses can be omitted when QuerySet is accessed from
multiple threads
-------------------------------------+-------------------------------------
Reporter: Marti Raudsepp | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: race-condition, | Triage Stage:
orm, threading, SQLCompiler | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
> Do document this! Most Django APIs seem to be thread-safe, how is a user
supposed to know, where the limits of thread (un)safety are?
I was under the impression that this was formally documented but just like
multi-processing gotchas when dealing with connections it doesn't seem to
be the case.
> So QuerySet.all() is thread-safe, but iterating over QuerySet is not?
How is one supposed to know?
Accessing `shared_qs: QuerySet` in a thread and calling `thread_qs =
shared_qs.all()` creates a copy of the queryset
[https://docs.djangoproject.com/en/4.1/ref/models/querysets/#all as
document] hence threads don't share the same instance.
> he only result that was sort-of authoritative was #11906 and since it's
marked as "fixed", it looked like this API aims to support thread safety.
I again don't agree with your conclusion here. If you read the ticket's
evolution it was marked as ''fixed'' because `QuerySet._fill_cache` was
removed during a refactor, not because thread safety guarantees for ORM
objects changed. There's even a comment in there that points to a page
that is now long gone and stated
> However, QuerySets are known not to be thread-safe, see #11906. Usually
that does not pose problems as '''they are (or should be) not shared
between threads in Django'''. '''The exception to that rule is the use of
exotic global/class-level/shared instance variable querysets in your own
code''' (e.g. when using the ORM outside of the Django dispatch system),
where '''you are assumed to know what you are doing and protect them
appropriately anyway'''.
--
Ticket URL: <https://code.djangoproject.com/ticket/34091#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070183d31256f3-fad1d721-91d6-4204-9cd7-50f22de849fb-000000%40eu-central-1.amazonses.com.