title: Is a Django session thread safe?
body: <p>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?</p>
  
  <p>Typical example:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Does there is a chance that when <code>Point2</code> update thread dictionary in session just after it updates <code>Point1</code> also update it, then my <code>stop</code> to quit thread is lost.</p>
  
  <p><strong>More Info</strong></p>
  
  <p>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.</p>