7 ms·
> Add a middleware that stores the session object in a thread-local variable for the duration of that request This has come up many times, and I've never been
by Demiurge 9y ago
> Add a middleware that stores the session object in a thread-local variable for the duration of that request
This has come up many times, and I've never been able to figure out how to do this without something redis. Do you know any open source examples of this?
- chronial 9y agoI am very confused how redis could get involved here. I hereby declare this open source :) import threading _local = threading.local() class SessionKeeperMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): _local.session = request.session response = self.process_request(request) _local.session = None return response def my_callback(): do stuff with _local.session
- Demiurge 9y agoWell, the idea is that I would have to store a variable and access it without races, so I would use an external storage library. This is pretty cool though, I've never used python threading for state, thanks a lot!
- filmor 9y agoThis doesn't use threading, just a thread-local variable (i.e. a variable that points to different memory locations for each thread) to make the whole thing threadsafe.