Category Archives: Python Programming

Managing Gunicorn Processes With Supervisor

Last week, I have written a post about gunicorn applications. We started gunicorn manually, and our application worked. Yay!

However, everything is not so great. When (If) the server reboots, gunicorn must be started manually, again. We should find a way to automate this. Actually, there are few ways to accomplish this, such as: init scripts, and supervisord.

Init scripts are more than enough. But they have some drawbacks. They are not so easy to write or maintain. If you have several projects, things get even harder. Supervisord is a relatively easier way for managing multiple gunicorn processes.

Let’s start with installing supervisor:

sudo apt-get install supervisor

Supervisor uses configuration files for applications located in /etc/supervisor/conf.d/ directory. The configuration for our application is below:

[program:hello]
command = gunicorn hello:app
directory = /home/username/
user = username

This is a very basic configuration required to run gunicorn. Since we are using virtualenv, we need to change “command” parameter to use python and gunicorn from our environment instead of global ones.

command = /home/username/hello/bin/python /home/username/hello/bin/gunicorn hello:app

Now we can test, whether our configuration works or does not. Reload supervisor with following commands and start our application. Stop gunicorn if it is running, and start it again with supervisor.

supervisorctl reread
supervisorctl update
supervisorctl start hello

Now gunicorn must be running, you can make sure by visiting page. If server reboots, supervisor starts it. If gunicorn fails, supervisor restarts it. Finally, everything is great!

You can find more information about supervisor configuration parameters in the documentation. You can also read my blog post about restarting and reloading supervisor.

How to Run Flask Applications with Nginx Using Gunicorn

We have recently bought a VPS for İTÜ24, the online newsletter of Istanbul Technical University. The server is running on Ubuntu Server 12.04 operating system. Due to limited memory resources and performance concerns, we preferred to setup nginx as web server.

Our server will serve several web pages and applications developed in various programming languages, such as PHP, Python, Ruby (on Rails). Currently, we have one Python application, which is using Flask framework.

How we run Flask application with nginx, step by step…
Continue reading

WikiLeaks Cablegate Count v2

Since the official WikiLeaks page has changed, the previous script needs to be updated. Here it is:

import urllib, piksemel, re

wleaks = int(re.search(r"(d+) / [0-9,]+", piksemel.parseString(urllib.urlopen("http://www.wikileaks.ch/cablegate.html").read()).getTag("body").getTag("div").getTag("div").getTag("a").nextTag().getTag("p").toString()).groups()[0])

Please remember to install piksemel module.

WikiLeaks Cablegate Count

Today, I wrote a small script to get the count of released WikiLeaks cables. Actually, it’s extremely small. Just two lines of code.

import urllib, piksemel, re

wleaks = int(re.search(r"(d+) / [0-9,]+", piksemel.parseString(urllib.urlopen("http://cablegate.wikileaks.org/index.html").read()).getTag("body").getTag("div").getTag("div").getTag("a").getTag("p").toString()).groups()[0])

Generally, I don’t code like this. But I wanted to write it quick (5 minutes?) and keep it short. It uses piksemel to parse the page.

Enjoy!

How to install piksemel module

piksemel is a easy to use python XML parser, based on iksemel.

Not: If you’re using Pardus, piksemel is preinstalled on your system.

Not: If you’re using Windows, before you continue make sure you’ve already installed mingw32 to your system and its directory is included in PATH.

Steps for Linux users:

  • Download the package from http://cekirdek.pardus.org.tr/~bahadir/piksemel/piksemel-1.3.1.tar.gz and unpack it.
  • Run:
    python setup.py build
  • Run:
    python setup.py install

Steps for Windows users:

  • Download the package from http://cekirdek.pardus.org.tr/~bahadir/piksemel/piksemel-1.3.1.tar.gz and unpack it.
  • In piksemel-1.3.1 directory, run:
    setup.py bdist_wininst build --compiler=mingw32
  • Run the installer file under the piksemel-1.3.1/dist directory.