In a Python Google App Engine environment, I have some highly volatile data which should be eventually saved. I would store it in global variables of an instance, but I need some sort of notice that the instance is being shutdown so I could write it out.
Is there such a callback or place to install a hook?
(memcache is an alternative, but I do not need the overhead of consistency between instances and it also shares the problem of vanishing without warning)
(for more on using global variables one can read about Cachepy in the Google Cookbook.)
You might want to take a look at how gae-sessions works. It adds a session middleware to the wsgi app so that data is persisted at the end of each request.
You could give each instance a unique id when it starts up, and always write the volatile data to an entity, with that unique id as a key name, at the end of each request. You would end up doing more writes, but you’d never lose the data to a crashed or terminated instance. You would never need to read the data into an instance, because it would persist for the instance’s lifetime (I think).