Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Context: I am developing a web application using webapp2 web-framework with python to be hosted on GAE. The web application (web-app from now on), is a chess variant. Being a first timer, I have definitely made a lot of design blunders. One of them, is what this question is regarding to, I will come to it in a short while.

The aim of the web-app was to allow quite a large number of players to play 2-player chess, such that multiple games are running in parallel. While not exceeding the free quota on GAE. (I am a student).

To do that, I thought that I would create a new game object, and store its id(game_object) along with the machine_id of the two players playing on that instance, in the datastore.

When querying the datastore with the machine_id of a player, I could get a id() value, and I thought could use that value as a key in a dictionary wherein a reference to the game_object forms the value.

Doing this, doesn't need me to write/update the datastore, hence saving on the precious writes. I could easily modify the game-state of the game_object, and it could be still referenced by the id().

But, now what happens is, this works fine for a few moves. After that the garbage collection takes place and the game_object is wiped off the memory. Raising a NameError exception.

My question is, can this be solved by creating new game_objects in a separate module where garbage collection is turned off, and then periodically/event-triggered freeing the memory? If not? How do I come around this problem?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You are not running into the Python garbage collection process here. Google App Engine doesn't guarantee that all requests go to the same instance. New instances do not share memory with old instances.

Load your data into memory again when your dictionary is empty.

All that the CPython garbage collector does is break circular references so that the normal reference count mechanism can remove objects from memory when no longer used. Your process has a dictionary referring to your data, and the garbage collector would never touch that data.

share|improve this answer
    
The web-app is still in development and is being hosted on the local machine. Google App Engine doesn't guarantee that all requests go to the same instance. Will that be an issue even on the local machine? –  tMJ Dec 9 '13 at 20:45
    
The local dev server supports multiple backend instances, so yes. –  Martijn Pieters Dec 9 '13 at 20:52
    
Aah! Ok. So is there no other way, than to store the game-state in the datastore in some or the other representation. –  tMJ Dec 9 '13 at 20:54
    
You can still cache stuff locally, or you could use the new MemCached implementation now available on GAE. –  Martijn Pieters Dec 9 '13 at 20:55
    
Is it safe to assume that the python garbage collector does not touch any python dictionary data-type? –  Ashish Nitin Patil Feb 25 at 23:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.