I am storing a dictionary in a Django Session which is accessible by multiple threads. All threads can update that dictionary, threads also get values from dictionary in order to run the process. I want to know does the Django Session is thread safe or I have to use locks or semaphores?
Typical example:
Thread1:
threadDict = request.session.get('threadDict', None)
if threadDict['stop']:
#break the for loop exit the thread
else:
#do some processing and update some values in thread dictionary
threadDict['abc'] = 0
request.session['threadDict'] = threadDict (Point1)
def someFunction():
#this function is used to send stop signal to thread
threadDict = request.session.get('threadDict', None)
threadDict['stop'] = True
request.session['threadDict'] = threadDict (Point2)
Does there is a chance that when Point2 update thread dictionary in session just after it updates Point1 also update it, then my stop to quit thread is lost.
More Info
An ajax request start four threads which download samples from the 4 different urls. Why i used threads? because i want to show the user which samples are currently being downloaded and which are left. All threads will update its state in dictionary within session. After threads started i make ajax request after every two seconds and take the dictionary from session and read the current state of threads. But this idea failed because threads are independent of request and their session. Each ajax request definately have its session but i can not pass that session to threads because when they once begin they are independent of rest of world (May be i can pass it but i may not pass it as fast the processsing is being done by the threads). so to tackle this problem i choose cache framework instead of session. as cache is accessable from any where. Threads store their state in dictionary and put back in to cache and after every two seconds i take dictionary from cache and read the state. And one more thing according to my experience cache is not thread safe. So for four threads i used four dictionaries sepratelly.
The
request.sessionobject returned will be a new one for every request, accessing the same storage. Furthermore, they are loaded from the storage at request time and saved back at response time. So if you wish to transfer information from a long-running thread to another request, you need to save it in the long-running thread manually. Unfortunately, this will result in modifications to the same session’s data being lost.Session is thread-safe in some sense. You will not break the interpreter in this way. Your request will see the session data as a snapshot of it on the first access, and on saving it will overwrite all changes that landed since that moment. So the session state will be consistent, but some requests’ modifications may be lost.
Actually this property is common for virtually any framework – any session/cache/some other storage that may be shared between multiple processes is very unlikely to provide modifications of the objects stored inside without the chance of someone’s changes being overwritten.