Running CherryPy based apps under Google's App Engine

Google's App Engine is a new application platform from Google that allows you to run custom Python code on Google's infrastructure. From their website:
Google App Engine lets you run your web applications on Google's infrastructure. App Engine applications are easy to build, easy to maintain, and easy to scale as your traffic and data storage needs grow. With App Engine, there are no servers to maintain: You just upload your application, and it's ready to serve your users.
Several websites (including Google) make the claim that App Engine is compatible with any WSGI framework, even mentioning CherryPy by name. Unfortunately, they don't give any examples using CherryPy, and it turns out that it doesn't quite work "out of the box". Fortunately it wasn't too tricky to get it working, so I documented the steps here.

Click through to read more ...

Prerequisites


  • Install Python 2.5 if you don't already have it.
  • Install the AppEngine SDK
    You do not need a Google App Engine account to begin developing and running your own apps locally.

Create your application environment


Create a new folder where your application will live. For the example here, I'm using c:\hello

As noted above, CherryPy doesn't quite work "out of the box". I was only able to get it work with the latest beta version, but even then I had to patch it a bit. I'm sure there is a more elegant way to accomplish this than what I did (simply removing the offending code), but this works for now.

For ease of use I've provided a prepatched version of CherryPy: If you'd rather do the patching yourself, here is the patch

Add your application code


Now to get down to business:
  • Create hello.py in your application folder (i.e. c:\hello):
    hello.py
    """
    Recipe for running a CherryPy-based server under the Google AppEngine.
    There is actually nothing AppEngine-specific here - the key is setting up
    the environment correctly by following the other steps.
    """
    import cherrypy
    import wsgiref.handlers

    # application goes here ...
    class Root:
        def index(self):
            return "Hello, CherryPy! version=",cherrypy.__version__
            
        index.exposed = True

    #---------------------------------------------------------------------------
    # Start the server under Google AppEngine
    #---------------------------------------------------------------------------
    app = cherrypy.tree.mount(Root(), "/")
    wsgiref.handlers.CGIHandler().run(app)
  • Create app.yaml in the same folder:
    app.yaml
    application: hello
    version: 1
    runtime: python
    api_version: 1

    handlers:
    - url: /.*
      script: hello.py


Running your application


Assuming you placed the AppEngine installation folder in your PATH (i.e. c:\Program Files\Google\google_appengine), you can run your server like this:
cd c:\hello
dev_appserver.py .


The first time you run an App Engine application, it will prompt you about whether it should automatically check for SDK updates. I answered "yes", but I don't think that's required.

You should now be able to view your application through http://127.0.0.1:8080. When you are finished, you can stop the application with Ctrl-C in the console window.

Ctrl-C does not immediately kill the application, at least for me. I have to press Ctrl-C then go to the browser window and reload the page to get App Engine to recognize the Ctrl-C.


I hope this brief tutorial has saved you some pain. Have fun!

Update (Apr 19, 2008): A kind Googler was nice enough to set me up with an App Engine account, so I've created a tutorial site at http://boodebr.appspot.com

Written in WikklyText.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

thanks very much

that's what i need now,thank you!

Thanks!

You did save me some pain + suffering... new to Python and CherryPy so this was much appreciated.

CherryPy 3.1 final

Just wanted to mention to you (and anyone who reads this ;) that the latest CherryPy release, 3.1.0, works with GAE with no modifications now. Thanks for investigating this and pointing out where the blockers were.

Assuming wsgiref.handlers.CGIHandler().run(app) blocks until you hit Ctrl-C, you should at least wrap that in a try/finally and call cherrypy.engine.stop() to shut down any other threads which CP started. I'd also recommend you call engine.start() just before run, but that will also require cherrypy.server.unsubscribe() to turn off the default HTTP server.

I'd be interested to know how the other engine plugins, like autoreload, daemonization, pidfiles, drop privs, and signal handling work (or not) in GAE. Depending on how much is restricted, we might invent a 'GAE' environment to go alongside 'production' and 'staging'.

Is there any example

Great, is there some example of proper hello world with 3.1?

CherryPy 3.1

Thanks for the work on 3.1! I noticed the GAE compatibility note in the latest release notes. I was just about to give this a try again with the 3.1-final. Another showstopper I hit was that sessions crash, even when setting the type to 'ram'. So I think having a "gae" session type will be a must-have. I'll try hacking on that a bit if I can.

SSL gone?

Thanks for posting that, I ran into that problem myself when trying to get CherryPy up and running on GAE. My only concern is that you have hacked out SSL support ... what if you want this? Although, I'm guessing that without OpenSSL libraries actually manually supplied into your app directory, it wouldn't have worked anyway so that SSL = None would have been what was happening.

SSL under GAE

Removing SSL support was a "get it working" patch, definitely not the ideal solution. My hope is that someone with more know-how about the CherryPy internals will figure out a better solution.

I'm a little stuck with CherryPy+GAE right now since sessions don't want to work. I need to dig back into that at some point.

Thanks for the tip...

I was too lazy to figure out why Ctrl-C did absolutely nothing... until I searched Googs and came upon your tip. Thanks! :)

thanks for your patch

I think CherryPy is suitable Application framework for Google App Engine.

Thanks for your reply and this patch. I invited you as developer my Google App test application.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.