web2py on appengine for production

24 views
Skip to first unread message

Robin B

unread,
Aug 4, 2008, 2:13:40 PM8/4/08
to web2py Web Framework
Here are some problems and solutions for running web2py on appengine.
I am using these techniques at http://ru.ly in production on
appengine.

1) cPickle does not correctly alias to pickle

It cPickle works in the SDK, but not on Google's servers:
http://code.google.com/p/googleappengine/issues/detail?id=284&q=pickle&colspec=ID%20Type%20Status%20Priority%20Stars%20Owner%20Summary

Solution, where cPickle is imported:
import pickle as cPickle

2) the wsgihandler is reloaded on *every* request

Solution, let Google cache main() in wsgihandler.py:

def main():
import sys, os
path=os.path.dirname(os.path.abspath(__file__))
if not path in sys.path: sys.path.append(path)

import gluon.main
application=gluon.main.wsgibase

if __name__ == "__main__":
main()

3) @cache caches POST requests by default

Since forms are usually self submitting, you have to be careful about
caching form submissions. Instead of checking for 'POST' in every
function, I made a ConditionalCache:

cache_enabled = True

def default_cache_conditions():
return cache_enabled and
request.env.request_method=='GET'

from gluon.cache import Cache
class ConditionalCache(Cache):
def
__call__(self,key=None,time_expire=300,cache_model=None,test=default_cache_conditions):
if not cache_model: cache_model=self.ram
if test:
def tmp(func):
if test():
return lambda: cache_model(key,func,time_expire)
else:
return func
else:
def tmp(func):
return lambda: cache_model(key,func,time_expire)
return tmp
cache = ConditionalCache(request)

4) no cron tasks for stale session clean cleanup

Solution, store sessions in memcache:

from gluon.contrib.memdb import *
from google.appengine.api.memcache import Client
session.connect(request,response,db=MEMDB(Client()))

This trick will work with google memcache and regular memcache, is
faster than hitting the datastore, and you never have to explicitly
cleanup the sessions.

If there is interest, I will submit patches once these features are
stable.

Robin

Massimo Di Pierro

unread,
Aug 4, 2008, 2:44:22 PM8/4/08
to web...@googlegroups.com
What is MEMDB? Is there a patch coming?

Massimo

Robin B

unread,
Aug 4, 2008, 7:12:30 PM8/4/08
to web2py Web Framework
MEMDB is a DAL wrapper around Memcache.

It is a hack that allows the DAL to insert,update,delete to a Memcache
Client as the backend.

It only only provides what memcache supports: get, set, and delete
using simple queries like:

db(db.table.id==my_id).select()

The good news is that sessions do not need more than insert, update,
delete, so sessions can use MEMDB.

I will clean it up and make a patch.

Robin


On Aug 4, 1:44 pm, Massimo Di Pierro <mdipie...@cs.depaul.edu> wrote:
> What is MEMDB? Is there a patch coming?
>
> Massimo
>
> On Aug 4, 2008, at 1:13 PM, Robin B wrote:
>
>
>
> > Here are some problems and solutions for running web2py on appengine.
> > I am using these techniques athttp://ru.lyin production on

Robin B

unread,
Aug 6, 2008, 12:52:18 AM8/6/08
to web2py Web Framework
I added doctests to memdb.py and uploaded it here:

http://code.google.com/p/web2py/issues/detail?id=21

Use with sessions like so:

from gluon.contrib.memdb import *
from google.appengine.api.memcache import Client
session.connect(request,response,db=MEMDB(Client()))


Robin

On Aug 4, 1:44 pm, Massimo Di Pierro <mdipie...@cs.depaul.edu> wrote:
> What is MEMDB? Is there a patch coming?
>
> Massimo
>
> On Aug 4, 2008, at 1:13 PM, Robin B wrote:
>
>
>
> > Here are some problems and solutions for running web2py on appengine.
> > I am using these techniques athttp://ru.lyin production on

Massimo Di Pierro

unread,
Aug 6, 2008, 2:27:09 AM8/6/08
to web...@googlegroups.com
in trunk under contrib.

Massimo

Reply all
Reply to author
Forward
0 new messages