Quantcast
Channel: The definitive Pythonmeister » Python
Viewing all articles
Browse latest Browse all 2

bottle.py and gunicorn

$
0
0

Yesterday I got hold of my new VPS from Host Europe.
The order and setup process went fast and well, and the Linux was properly set up, with
no bloat installed, just a minimal Debian stable.
I took the smallest model, giving me 2 GB RAM, 1 vCPU and 50 GB disk space - enough to play around with
some ideas I have for *my* perfect todo application.
The server runs on reasonable DELL hardware, and is fast enough and RAM is plenty -
or so I thought ...

I just installed the usual suspects: Python 2.6, bootle.py, PostgreSQL, memcached - and Apache 2.2.22,
hacked together a bottle.py skeleton, configured Apache and - got scared.

This Apache beast is eating my RAM - without mod_wsgi about 86 MB virtual, and with mod_wsgi whopping 250 MB.

So I de-configured the mod_wsgi and looked for alternatives.
I did them all but found gunicorn was the one for me.
It works really well, uses not much memory, and is fast - even on a VPS with one processor.

Gunicorn serves about 1300 req/s on this server, the example application is this one:

#!/usr/bin/python

from bottle import route,run,error,Bottle
import memcache
cache = memcache.Client(('127.0.0.1:11211',))
cache.set('count',0)

app = Bottle()

@app.route('/<name:re:[a-z]*>')
def index(name='Test'):
    while True:
        count = cache.get('count')
        if cache.cas('count',count+1):
            break
    return 'Hello, %s: %i' % (name,cache.get('count'))

@app.error(404)
def error404(error):
    return 'Nothing here, sorry'

It took me some time, to get gunicorn working (correctly), so here is the working config file
/etc/gunicorn.d/demo.conf:

CONFIG = {
    'mode': 'wsgi',
    'working_dir': '/var/www/demo',
    'python': '/usr/bin/python',
    'args': (
        '--bind=:8000',
        '--workers=16',
        '--timeout=60',
        '--user=www-data',
        '--group=www-data',
        'main:app',
    ),
}

The next step is to get rid of Apache completely and replace it with nginx.

Because: even in 2013 2 GB RAM should be enough to serve a few thousand visitors a day
on a reasonable well developed application without CPU starvation or swapping memory to disk.
With the setup (PostgreSQL 9.1, Python 2.6, memcached (no data yet), bottle.py, gunicorn) the memory usage
is around 300 MB, which leaves room for improvement.

UPDATE:
Every comment about how to strip Apache down to reasonable resource usage is a waste of time.
I WILL NOT LISTEN!


Viewing all articles
Browse latest Browse all 2

Trending Articles