Read -> change -> save. Thread safe
memcache is 'just' a cache, and in its usual guise it's not suitable for an atomic data store, which is what you're trying to use it for. I'd suggest using the GAE datastore instead, which is designed for this sort of issue.
This code should automatically connect players when they enter a game.
But the problem is when two users try to connect at the same time - in this case 2nd user can easily overwrite changes made by 1st user ('room_1' variable).
How could I make it thread safe?


def join(userId):
    users = memcache.get('room_1')
    users.append(userId)
    memcache.set('room_1', users)
    return users


I'm using Google App Engine (python) and going to implement simple game-server for exchanging peers given by Adobe Stratus.