Tag Archives: gevent

VMWare Workstation start on boot CentOS

CentOS Gunicorn installation instructions for newbies

centos gunicorn installation instructions

centos gunicorn

centos gunicorn : In this simple tutorial I’ll explain how to install and run Gunicorn python server on your CentOS machine.

This tutorial is meant for Centos 6.4 and above but it should work on any CentOS 6.x release.

 

What is gunicorn?

From: Gunicorn – Python WSGI HTTP Server for UNIX:

Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.

 

Installation

Python

before you going to install the centos gunicorn server you need to insure you have the python interperter installed (it should be, as it’s installed by default on the latest CentOS releases). to check that you have python use the command:

python

command not found of course means you need to install python now.

 

Install python

first make sure your have python installed. Simple as:

yum install python

test python by typing: “python” and you should see something similar to:

python

(Ctrl+D to exit)

 

Gunicorn

Next we need to install gunicorn. for this we have (as always) several choices.

1) Using YUM. I personally don’t recommend it. I know some are happy to use the system packaging management wherever possible, but as for python I don’t think it’s the way to go.

To install gunicorn using yum:

yum install python-gunicorn

2) Using easy_install. using easy_install is a better choice for my taste to install python packages. this is how you install gunicorn using easy_install, but I recommend installing gunicorn using PIP as I will show next…

yum install python-setuptools
easy_install gunicorn

3) Using PIP: This is my RECOMMENDED way of installing gunicorn. to install PIP you actually need easy_install so the commands are:

yum install python-setuptools
easy_install pip
pip install gunicorn

Virtualenv

I can’t recommend enough to start working with virtualenv from beginning. At some point in the future, if you are going to consist with python coding, you are going to use the ‘pip update’ command to update some or all your libraries and then expect your python application to stop working.

Python libraries, as any open source library, have the freedom to sacrifice backward compatibility for new features, performance or redesign.

Virtualenv is a python virtual environment tool to ensure that your python applications will work as expected as long you don’t deliberately update some or all of the dependency libraries for that virtual environment specifically.

To install virtualenv (and virtualenvwrapper) and learn the basics read this tutorial. then create your virtual environment and install gunicorn to that environment.

# after you've install pip, virtualenv and virtualenvwrapper
mkproject myapp
workon myapp
pip install gunicorn

 

Running Gunicorn

Basic usage:

$ gunicorn [OPTIONS] APP_MODULE

so for the following myapp.py file:

# -*- coding: utf-8 -

def app(environ, start_response):
    data = 'Hello, World!\n'
    status = '200 OK'
    response_headers = [
        ('Content-type','text/plain'),
        ('Content-Length', str(len(data)))
    ]
    start_response(status, response_headers)
    return iter([data])

just run:

gunicorn -w 4 myapp:app
# Sample output
[INFO] Arbiter booted
[INFO] Listening at: http://127.0.0.1:8000
[INFO] Worker spawned (pid: 16801)
[INFO] Worker spawned (pid: 16802)
[INFO] Worker spawned (pid: 16803)
[INFO] Worker spawned (pid: 16804)
centos gunicorn

centos gunicorn

This command starts gunicorn with 4 workers on port 8000. feel free to experiment and customize the command with additional parameters.

There also command line for using Django <1.4 and Paster. read more here.

Read more about configuring Gunicorn and configuration files.

 

Deploying Gunicorn

on deployment, It’s strongly recommended to use Gunicorn behind a proxy server.

nginx

I personally prefer nginx but it’s not your only choice.

If you want to install nginx on your CentOS machine follow this installation instructions.
use this script (at github) to configure your nginx to pass request to the gunicorn process

 

Monitoring and Logging

Usally you’d want the gunicorn to be run in the background, load on boot and restart on errors. you want also the ability to monitor that process.

There are several tools for that.job, including: Supervisord, Gafferd, runit and many more…  choose what fits you best. here you have examples for monitoring gunicorn using those tools.

 

That it. enjoy playing with your centos gunicorn setup.

 

Jinja2 vs Tenjin: Why I moved (back) from Jinja2 to Tenjin

Jinja2 vs Tenjin: The Story

I started a new project: for learning purposes: to learn python gevent api and to implement a simple server – with Authentication & Websockets.

First thing after handling routings and starting server with the famous ‘hello, world!’ message was to find a suitable Framework, if any.

After a search I found Bottle which I think is amazing microframework. Flask was also great, but I loved the idea of a microframework without template engine so I could choose my own.

As I worked with many Template engines in many different languages and environments (.NET/Ruby/Node.js/Python/PHP/and more). I know I can handle any syntax, so the first thing I’ve looked for is the fastest Python template engine.

I’ve choosed to try Tenjin, It worked great, looked bad. the syntax was messy, there was that .cache file near my.pyhtml files… But it worked great and FAST!

I’ve got to admit that SpitFire catched me, and I’d choose it for several reasons:

  • Really FAST!!
  • Trivial Cheetah templates will probably compile in Spitfire

But as I ‘m not using Cheetah and the Languages support Tenjin offers – for me It is Winning case for Tenjin.

 

Jinja2 vs Tenjin

I decided to try more template engine, slower – but friendly : Jinja2.

Tenjin syntax , which was somewhat ugly, and Jinja2 was just beautirful, and reminds me Django’s and Twig which I like… So, I;ve decided to try Jinja2 and to feel the difference.

Found no problems there, Jinja2 was working great, and the .html files are looking great in compare to Tenjin,.. more readable, no .cache file – and .html instead of .pyhtml. I’ve choosed to stick with Jinja2… for now…..

…meanwhile.. on another project I’m working on in Django, I’ve tried to implement Googles AngularJS and found my self in a very bad situation. the AngluarJS Template Engine was similar to Django’s one! There are several solutions and fixes for this, some are really messy but I fixed it… and I’ve learned a very important lesson… Templates engines used today in both sides: both client & server, and this should be taken into consideration when choosing template engines.

The true title of this article should be: Why I moved from Tenjin to Jinja2 and why I moved from Jinja2 to Tenjin again..

As Jinja is very similar to Django (and angular) I found my self thinking about Tenjin again…

  • It’s faster then Jinja.. it’s faster then most teamplte engines!.. ,
  • it support many languages which make the project more trasferable and convertable between servers (Supports PHP, Perl, Ruby & Python).
  • It supports JS templating – wchich I haven’t tried yet,
  • It’s different then the mainstream template engines with {{ }} and {*  *} so mixing Template engines with client side template engines should be safer.

  

Summary

I know it’s not a classical -vs- article which compares between features, and there is a lot to cover in the topic of jinja2 vs tenjin and the feautres of both,.. I choosed to wrote instead on my view and decisions… what made me go back to Tenjin and leave Jinja 2 behind.. This is my story of Jinja2 vs Tenjin… In my case, Tenjin won, but I MUST say that Jinja2 is GREAT!, also: SpitFire, Mako Template, Cheetah, Genshi & others… as always: each tool for different task… It’s your call…

 

 EDIT:

I learned about Jinja2 inheritance feature which is missing in Tenjin. If you need Template Inheritance go for Tenjin (or other).

 

What do you think? Which Python Template engine do you use? and why?

gevent CentOS 6.x Installation Guide

gevent CentOS 6.x Installation Guide

gevent CentOS 6.x Installation Guide

Installation

Requirments

  • yum install python python-devel
  • yum install libevent-devel

 

Install Setuptools

install python-setuptools (for easy_install)

yum install python-setuptools

Install greenlet

Install greenlet:

easy_install greenlet

Install gevent

There are several ways to install gevent:

using easy_install (recommended)

easy as:

easy_install gevent

centos gevent

from source

You need wget to download source:

yum install wget

Now, installing gevent: (Check here for the latest package)

cd /tmp wget http://pypi.python.org/packages/source/g/gevent/gevent-0.13.8.tar.gz tar -xvzf gevent-0.13.8.tar.gz cd gevent-0.13.8 python setup.py install

U may need to add –no-check-certificate to the wget if something is wrong.

from GitHub

You can also install gevent from github using pip.

Test

to test simply run in bash:

python

and then:

import gevent

if no error found, you have installed it correctly python-import-gevent

First App

Example taken from the community documentation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from gevent.wsgi import WSGIServer
 
def application(environ, start_response):
    status = '200 OK'
    body = '<p>Hello World</p>'
 
    headers = [
        ('Content-Type', 'text/html')
    ]
 
    start_response(status, headers)
    return [body]
 
WSGIServer(('', 8000), application).serve_forever()
from gevent.wsgi import WSGIServer

def application(environ, start_response):
    status = '200 OK'
    body = '<p>Hello World</p>'

    headers = [
        ('Content-Type', 'text/html')
    ]

    start_response(status, headers)
    return [body]

WSGIServer(('', 8000), application).serve_forever()

copy the example into a file (server.py for ex), and start the server by:

python server.py

and that’s it! browse to the machine on port 8000 using something like this:

http://[ip of the machine]:8000

gevent-working

What’s next..?

 

Production

In production you can host your gevent after several WSGI server racks like: Gunicorn, Circus, Meinheld, uWSGI, etc..

Gunicorn is the most common WSGI server to host gevent applications. one benchmark shows that Circus is faster then Gunicorn. other tested uWSGI vs Gunicorn and found that uWSGI is faster.

recommend you run them behind a reverse proxy such as nginx. Additional advantages of running them behind a reverse proxy include improved handling of slow clients and bad HTTP requests

 

SocketIO

You need socketio? you may want to install it:

easy_install gevent-socketio

 

Read more…

 

need microframework? (flask/bottle)?

You can use a python web framework on top of gevent.