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 7903097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:39:42+00:00 2026-06-03T09:39:42+00:00

I face a potential race condition in a web application: # get the submissions

  • 0

I face a potential race condition in a web application:

# get the submissions so far from the cache
submissions = cache.get('user_data')
# add the data from this user to the local dict
submissions[user_id] = submission

# update the cached dict on server
submissions = cache.update('user_data', submissions)

if len(submissions) == some_number:
    ...

The logic is simple, we first fetch a shared dictionary stored in the cache of web server, add the submission (delivered by each request to the server) to its local copy, and then we update the cached copy by replacing it with this updated local copy. Finally we do something else if we have received a certain number of pieces of data. Notice that

submissions = cache.update('user_data', submissions)

will return the latest copy of dictionary from the cache, i.e. the newly updated one.

Because the server may serve multiple requests (each in its own thread) at the same time, and all these threads access the shared dictionary in cache as described above, thus creating potential race conditions.

I wonder, in the context of web programming, how should I efficiently handle threading to prevent race conditions in this particular case, without sacrificing too much performance. Some code examples would be much appreciated.

  • 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-06-03T09:39:43+00:00Added an answer on June 3, 2026 at 9:39 am

    My preferred solution would be to have a single thread that modifies the submissions dict and a queue that feed that thread. If you are paranoid, you can even expose a read-only view on the submissions dict. Using a queue and consumer pattern, you will not have a problem with locking.

    Of course, this assumes that you have a web framework that will let you create that thread.

    EDIT: multiprocess was not a good suggestion; removed.

    EDIT: This sort of stuff is really simple in Python:

    import threading, Queue
    
    Stop = object()
    
    def consumer(real_dict, queue):
        while True:
            try:
                item = queue.get(timeout=100)
                if item == Stop:
                    break
                user, submission = item
                real_dict[user] = submission
            except Queue.Empty:
                continue
    
    q = Queue.Queue()
    thedict={}
    
    t = threading.Thread(target=consumer, args=(thedict,q,))
    t.start()
    

    Then, you can try:

    >>> thedict
    {}
    >>> q.put(('foo', 'bar'))
    >>> thedict
    {'foo': 'bar'}
    >>> q.put(Stop)
    >>> q.put(('baz', 'bar'))
    >>> thedict
    {'foo': 'bar'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I face a problem when I try to get value from primary table in
Q: I face the following problem only when i tried my web site from
I face troubles accessing my website deployed on Websphere Application Server 6.0 when I
I often face the problem of wanting to add additional methods to classes I
I just started using @font-face This is on top of my css @font-face {
I am currently creating a web application that will be deployed in an intranet
I face a situation where my web service needs to access partion D in
I have vertex and triangle data which contains a color for each triangle (face),
I face the following exception: weblogic.transaction.internal.TimedOutException: Transaction timed out after 300 seconds this is
I face this often. Suppose I have killed a buffer. And then I go

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.