4 ms·
I think the parent misunderstood "inability to get it passed through the Django module to the callback". At first it sounded to me like you were trying one of y
by chronial 9y ago
I think the parent misunderstood "inability to get it passed through the Django module to the callback". At first it sounded to me like you were trying one of your own values.
But if you actually plan to use this code in production, I would strongly advise against it :). There is a better solution available that will survive django upgrades:
Add a middleware that stores the session object in a thread-local variable for the duration of that request. You can then access that variable from your event handler.
- 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.