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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:36:31+00:00 2026-06-11T04:36:31+00:00

The Tastypie documentation states that bundles keep Tastypie more thread-safe but does not explain

  • 0

The Tastypie documentation states that bundles keep Tastypie more thread-safe but does not explain how and under what conditions. I have looked through the code however am not experienced enough to wrap my head around it.

I am prototyping a game that has a round object (for each round of play) and multiple states for each round (for each player’s information for that round). Each player updates their own state with an answer to the rounds word-phrase. I need a mechanism that lazily creates the next round of play if it doesn’t already exist. I currently trigger that round creation when a player updates their state.

If multiple players update their state (see StateResource.obj_update()) at the same time then could their attempt to create the next round collide? I am thinking that this could happen if one obj_update call checks to see if the next round exists and tries to create a next round before a different obj_update finishes creating a next round. I would solve this with some type of mutex but I’m not sure if that’s necessary. I’m wondering if there is a Tastypie-way to solve this.

My code is as follows:

#models.py
class Round(models.Model):
    game_uid = models.CharField(max_length=75)
    word = models.CharField(max_length=75)
    players = models.ManyToManyField(User)
    next_round = models.OneToOneField('self',null=True,blank=True)

class PlayerRoundState(models.Model):
    player = models.ForeignKey(User)
    round = models.ForeignKey(Round)
    answer = models.CharField(max_length=75)

#api.py
class RoundResource(ModelResource):
    players = fields.ManyToManyField(UserResource, attribute='players',full=False)
    states = fields.ManyToManyField('wordgame.api.StateResource',
                                attribute='playerroundstate_set',
                                full=True)
    . . .
    def obj_create(self, bundle, request=None, **kwargs):
        bundle = super(RoundResource, self).obj_create(bundle, request,**kwargs)
        bundle.obj.word = choice(words) #Gets a random word from a list
        bundle.obj.round_number = 1
        bundle.obj.game_uid = bundle.obj.calc_guid() #Creates a unique ID for the game
        bundle.obj.save()
        return bundle

class StateResource(ModelResource):
    player = fields.ForeignKey(UserResource, 'player',full=False)
    round = fields.ForeignKey(RoundResource, 'round')
    . . . 
    def obj_update(self, bundle, request=None, skip_errors=False, **kwargs):
        bundle = super(StateResource, self).obj_update(bundle, request,
                                                   skip_errors, **kwargs)
        if bundle.obj.round.next_round is None:
            new_round = Round()
            new_round.word = choice(words)
            new_round.round_number = bundle.obj.round.round_number + 1
            new_round.game_uid = bundle.obj.round.game_uid
            new_round.save()
            for p in bundle.obj.round.players.all():
                new_round.players.add(p)
            new_round.save()
            bundle.obj.round.next_round = new_round
            bundle.obj.round.save()

        return bundle
  • 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-11T04:36:34+00:00Added an answer on June 11, 2026 at 4:36 am

    I think this doesn’t have to do much with Tastypie.

    The problem you’re describing is related to the ORM and the database rather. The issue is that both requests could create new Round() under some circumstances (if they could be served in parallel and switch at any time which is the case with gunicorn and gevent) and one would become stale.

    Consider the following situation:

    First request arrives, retrieves the current round and “sees” that there is no “next” round. So it performs:

    new_round = Round()
    new_round.word = choice(words)
    new_round.round_number = bundle.obj.round.round_number + 1
    new_round.game_uid = bundle.obj.round.game_uid
    new_round.save()
    

    In the meantime a second request comes and (assuming it is possible in your setup) processing switches to that second request. It also retrieves the current round, and it also “sees” there is no next round, so it creates one too (a second object for the same logical round).

    Then the processing switches back to the first request which performs:

    for p in bundle.obj.round.players.all():
        new_round.players.add(p)
    new_round.save()
    bundle.obj.round.next_round = new_round
    bundle.obj.round.save()
    

    So now there “is” the next round. First request is processed and everything looks nice.
    But the second request has yet to finish, and it performs the very same operation, overwriting the current round object.

    The result is that you have one stale instance (the Round created by the first request) and that the first group of players uses a different Round than the second group.

    This leads to the inconsistent state in the database. So in such case your resource update method is NOT thread-safe.

    One solution to this would be to use select_for_update for retrieving the current round from the database. See Django Docs. If you used that, the second and consecutive requests would wait until you modify the current round in first request and only then retrieve it from the database. The result would be that they would already “see” the next round and not attempt to create it. Of course you’d have to make sure that the whole update constitutes one transaction.

    The way to “use” it would be to override obj_get() method in the StateResource resource and instead of:

    base_object_list = self.get_object_list(request).filter(**kwargs)
    

    use (haven’t tested):

    base_object_list = self.get_object_list(request).select_for_update().filter(**kwargs)
    

    Of course this is not the only solution but others would probably involve redesigning your application so this might just be less involving.

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

Sidebar

Related Questions

I've got a simple Django-TastyPie API defined (it is a basic class that derives
Does anyone can help about to serialize my tastypie api to response with plist
I am using backbone-tastypie, but I am having the toughest time getting it to
name 'entry_resource' is not defined This is my models.py from tastypie.utils.timezone import now from
I am following that tutorial http://thecodachi.blogspot.com/2012/03/django-tastypie-with-android-client.html and https://github.com/Mbosco/tastypie-api-example I did all things in the
I'm using TastyPie for Geo-distance lookups. That is a bit difficult, because oficially its
Using tastypie, how do I only authorize authors of objects the ability to edit/delete
I am currently using tastypie with 2 apps. Each of those apps has a
I have a REST API using Django Tastypie. Given the following code The models
I am now quite familier with tastypie , it is tightly coupled with authorization

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.