Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Django session thread safe?

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.

like image 888
Aamir Rind Avatar asked Nov 21 '11 12:11

Aamir Rind


2 Answers

The request.session object 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.

like image 191
Tanriol Avatar answered Oct 10 '22 19:10

Tanriol


Django session is a dictionary fetched at the start of the request processing and saved back at the end of it [source]. Therefore if you perform parallel changes to it, then one of those changes is going to prevail. But typically a human user cannot perform such parallel actions and the business just goes on.

Now, I don't quite get your example but it looks like you are simply trying to abuse the session dictionary. I've developed quite a few of Django-based websites, I've stucked on a number of problems related to the limitations or bugs inside Django itself, but still messing with the session dict from multiple threads looks to me like a textbook example of misuse of the session..

Maybe we could figure out the solution when you write something more about what you are trying to achieve?

like image 20
Tomasz Zieliński Avatar answered Oct 10 '22 19:10

Tomasz Zieliński