Search code examples
imagegoogle-app-enginesessionwebapp2

GAE webapp2_extras session memory limits (storing a temporary image in a session)


webapp2_extras' sessions are pretty useful, and I've been using them to keep users logged in and such. But there's not much documentation about it. For instance: which is the memory limit for an entry in the session dictionary?

So far I've stored strings and numbers. But I need to store a whole image, uploaded from a HTML form (the idea is that this image is temporary and it may be discarded, so I don't want to store it in the datastore until I'm sure I have to do it). It doesn't seem to work, and I guess I'm hitting a memory problem:

self.session['photo_image']  = self.request.get("photo_image")

Is that so? I guess there are memory limits, but I can't find them. How else could I store a temporary image in GAE?


Solution

  • You can store it in 'instance memory', e.g. create a global list when your script starts up and append incoming images to that, then remove it once done. Of course, you'll soon run of of memory there also if you have lots of users/large files. And you'll lose it all when the instance shuts down and you'll (might) have problems if more then one instance is running.

    So Memcache sounds perfect for this. using Memcache

     def get_data():
        data = memcache.get('key')
    
        if data is not None:
            return data
        else:
            data = self.query_for_data()
            memcache.add('key', data, 60)
            return data
    

    Yes, it's not 100% reliable as I'm sure you've heard but if you are not using 100's of files that are huge and keeping them round for ages you probably won't have issues. As it's first in first out (IIRC) if you process them more or less in order that'll be even less likely to lose data. I think memcache is going to be your best option here, try it.