I have a a static class attribute that gets modified in the same http request.
class A:
x = {}
def fun(k,v):
A.x[k] = v
My issue is that when your do another http request, the last value of the previous request persists.
Am using Django through Apache's mod WSGI .
How can I make the static value persists in the same request but not to another request?
The whole point of static variables is for them to persist in the class rather than the instances used to handle a particular request. This is dangerous when using threader or event-based servers as the static variable will be shared not only with the next request but also with all requests handled in parallel.
I assume class
A
here is a class-based view. In that case, you can change your attribute to be an instance one instead:As class-based views are re-instantiated for each request they serve, the value will not bleed into other requests.
Inspired by muddyfish's answer I implemented a middleware's process_response
Thank you all for your responses.
At the end of each request, clear the cache