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

  • Home
  • SEARCH
  • 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 119053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:35:31+00:00 2026-05-11T03:35:31+00:00

I’ve hit a small dilemma! I have a handler called vote; when it is

  • 0

I’ve hit a small dilemma! I have a handler called vote; when it is invoked it sets a user’s vote to whatever they have picked. To remember what options they previously picked, I store a VoteRecord options which details what their current vote is set to.

Of course, the first time they vote, I have to create the object and store it. But successive votes should just change the value of the existing VoteRecord. But he comes the problem: under some circumstances two VoteRecords can be created. It’s rare (only happened once in all 500 votes we’ve seen so far) but still bad when it does.

The issue happens because two separate handlers both do essentially this:

query = VoteRecord.all().filter('user =', session.user).filter('poll =', poll)  if query.count(1) > 0:  vote = query[0]   poll.votes[vote.option] -= 1  poll.votes[option] += 1  poll.put()   vote.option = option  vote.updated = datetime.now()  vote.put() else:  vote = VoteRecord()  vote.user = session.user  vote.poll = poll  vote.option = option  vote.put()   poll.votes[option] += 1  poll.put()   session.user.votes += 1  session.user.xp += 3  session.user.put()   incr('votes') 

My question is: what is the most effective and fastest way to handle these requests while ensuring that no request is lost and no request creates two VoteRecord objects?

  • 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. 2026-05-11T03:35:32+00:00Added an answer on May 11, 2026 at 3:35 am

    The issue is this part:

    if vote.count(1) == 0:     obj = VoteRecord()     obj.user = user     obj.option = option     obj.put() 

    Without a transaction, your code could run in this order in two interpreter instances:

    if vote.count(1) == 0:     obj = VoteRecord()     obj.user = user   if vote.count(1) == 0:     obj = VoteRecord()     obj.user = user     obj.option = option     obj.put()       obj.option = option     obj.put() 

    Or any weird combination thereof. The problem is the count test runs again before the put has occured, so the second thread goes through the first part of the conditional instead of the second.

    You can fix this by putting the code in a function and then using

    db.run_in_transaction() 

    to run the function.

    Problem is you seem to be relying on the count of objects returned by a query for your decision logic that needs to be put in the transaction. If you read the Google I/O talks or look at the group you’ll see that this is not recommended. That’s because you can’t transactionalize a query. Instead, you should store the count as an entity value somewhere, query for it outside of the transaction function, and then pass the key for that entity to your transaction function.

    Here’s an example of a transaction function that checks an entity property. It’s passed the key as a parameter:

    def checkAndLockPage(pageKey):   page = db.get(pageKey)   if page.locked:     return False   else:     page.locked = True     page.put()     return True 

    Only one user at a time can lock this entity, and there will never be any duplicate locks.

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

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My experience is compiling my C++ JPEG-LS image compression project.… May 13, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer I think your conceptual problem is that you're thinking of… May 13, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer North... I first saw the Object literal pattern in the… May 13, 2026 at 4:28 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.