Nicolas314

All my geeky stuff ends up here. Mostly Unix-related

gunicorn basics

leave a comment »

Following up on a previous post about fapws3 and its inability to spread the load on several workers, here is a quick rundown of how to get a gunicorn instance running in a matter of minutes.

If you do not want to disturb your system’s Python installation, virtualenv is the perfect tool. It will re-create the equivalent of a chrooted environment for Python stuff only, allowing you to mess up as much as you want with libraries without having to trash system libraries.

$ mkdir webtests
$ cd webtests
$ virtualenv app
New python executable in app/bin/python
Installing distribute..................................................................................................................................................................................done.
$ . app/bin/activate
$ easy_install -U gunicorn
Searching for gunicorn
Reading http://pypi.python.org/simple/gunicorn/
Reading http://github.com/benoitc/gunicorn
Reading http://www.gunicorn.org
Reading http://gunicorn.org
Best match: gunicorn 0.12.1
Downloading http://pypi.python.org/packages/source/g/gunicorn/gunicorn-0.12.1.tar.gz#md5=6540ec02de8e00b6b60c28a26a019662
Processing gunicorn-0.12.1.tar.gz
Running gunicorn-0.12.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-gf0pif/gunicorn-0.12.1/egg-dist-tmp-8ToCBM
Adding gunicorn 0.12.1 to easy-install.pth file
Installing gunicorn_paster script to /home/nicolas314/webtests/app/bin
Installing gunicorn script to /home/nicolas314/webtests/app/bin
Installing gunicorn_django script to /home/nicolas314/webtests/app/bin
Installed /home/nicolas314/webtests/app/lib/python2.6/site-packages/gunicorn-0.12.1-py2.6.egg
Processing dependencies for gunicorn
Finished processing dependencies for gunicorn

Now let us write a default Python webapp with two classes: immediate responds immediately to /im requests and delayed simulates a long-running task responding to /de: sleeps for 10 seconds and returns.

hello.py contains:

import time, web
class immediate:
    def GET(self):
        return 'immediate'
class delayed:
    def GET(self):
        time.sleep(10)
        return 'delayed'
urls = ('/im', 'immediate',
            '/de', 'delayed')
application = web.application(urls, globals(), True).wsgifunc()

Startup gunicorn with 10 workers on localhost:

gunicorn -w 10 hello

Open a browser, point one tab to localhost:8080/de and the other one to localhost:8080/im. The delayed task does not impact (directly) immediate responses. QED.

gunicorn has been benchmarked with excellent performance and will take care of all multi-worker stuff for you. Integration with web.py is excellent and painless. Installation is just a couple of commands. Congratulations to the gunicorn team!

References:

Advertisements

Written by nicolas314

Wednesday 6 April 2011 at 11:30 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: