Thread safety with view arguments in class based views

46 views
Skip to first unread message

Chris B

unread,
May 16, 2012, 7:18:49 PM5/16/12
to django...@googlegroups.com
According to the docs on the generic class based views:

Each request served by a View has an independent state; therefore, it is safe to store state variables on the instance (i.e., self.foo = 3 is a thread-safe operation).

and
Any argument passed into as_view() will be assigned onto the instance that is used to service a request.  
but then right after that, it says: 

Arguments passed to a view are shared between every instance of a view. This means that you shoudn't use a list, dictionary, or any other variable object as an argument to a view. If you did, the actions of one user visiting your view could have an effect on subsequent users visiting the same view.


I'm confused then.  Under what conditions do arguments passed to ClassView.as_view() become bound to that instance vs when do tehy get bound to the class as a whole (between requests)?

Jon Paugh

unread,
May 16, 2012, 10:51:43 PM5/16/12
to django...@googlegroups.com
That last part refers to the way Python handles arguments and the like. Whenever you have a complex object, such as an array, Python does not make a new copy of the object each time the view function is called. Instead, it passes a reference to the object, which means that if you change that object inside the view, it changes the original, which is shared across multiple calls to the same function.

If you don't modify the object, then all is well.

Rafael Durán Castañeda

unread,
May 17, 2012, 1:33:59 PM5/17/12
to django...@googlegroups.com
El 17/05/12 04:51, Jon Paugh escribió:
--
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/-/Fr2hbFuJOwoJ.
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.
I think is not an issue about thread safety and where docs says 'variable object' it really means mutable objects, thus any change on the object will affect any variable referencing it. Simple Python example:

def test(x, y=[]):
    y.append(x)
    print y

>>> test(3)
[3]
>>> test(4)
[3,4]

Chris B

unread,
May 17, 2012, 4:11:57 PM5/17/12
to django...@googlegroups.com
Alright. That makes sense. Thanks everyone

Rafael Durán Castañeda

unread,
May 17, 2012, 5:17:15 PM5/17/12
to django...@googlegroups.com
El 17/05/12 22:11, Chris B escribió:
--
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/-/UBbrae7PJi8J.

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.
I'm wondering if this is suitable for a doc bug report, it is?

akaariai

unread,
May 18, 2012, 1:03:55 AM5/18/12
to Django users
On May 18, 12:17 am, Rafael Durán Castañeda
<rafadurancastan...@gmail.com> wrote:
> I'm wondering if this is suitable for a doc bug report, it is?

I think a direct github pull request is the way to go. And yes, the
docs could be clarified here.

- Anssi
Reply all
Reply to author
Forward
0 new messages