Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:33:58+00:00 2026-05-27T14:33:58+00:00

I am storing a dictionary in a Django Session which is accessible by multiple

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:33:59+00:00Added an answer on May 27, 2026 at 2:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,
I'd like to set a cookie via Django with that has several different values
I'm trying to iterate over a dictionary of model values in a Django template
I have a simple Dictionary(of String, Object) that I need to iterate through and
In .NET can I use any string as a dictionary key? This is part
How does performance for reading/adding values from/to Dictionary(Of String, SomeReferenceType) depend on the number
I'd like to create a Dictionary that is indexed by Strings: Dictionary(of String, ...)
In version 1.0 i am using NSMutableArray and inside that i am storing NSNumbers
I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and
I am using the below mentioned library to create a barcode which is storing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.