My Python and Django bookmarks


This a list of Django and Python websites I found useful, I hope you find it useful as well, and may be add to it in the comments:

Deploying Django

Django is a web framework written in Python language, my favourite computer programming language.

Python a language is really great language, easy to read and understand, and very easy to learn, but for me I always found setting up Python for web development as a challenge, socially if compare it to PHP, which almost works out of the box, actually most people think PHP works only in the web server and can’t work as stand alone application, search for PHP CLI if you are interested.

I will try here to document an easy Django deployment for my future reference and hopefully it will help others: Continue reading Deploying Django

Faster file syncing with Redis

The problem
In most of the site administration take place inside the premises of Alriyadh Newspaper offices, and as you can image the bandwidth dedicated to the website team isn’t that big. This why we designed our system to have two parts, one inside Alriyadh Newspaper internal data center where the local access is very fast, and another part accessible to the public hosted in MeduNet, and we would have a database replication for the website data, and file system replication for the web site images and media.

Current Solution
MySQL replication is straight forward and very easy to setup, and since we are mostly replicating textual data, the replication is relatively fast and reliable, given that you have a reliable Internet connectivity.

For images replication we used “rsync” that run from a cron job, rsync is very efficient with bandwidth consumption, but it had to a scan a large number of directories to see which files aren’t synced, and this usually very slow process, and this why it would take couple of minutes for some images to appear.

Redis to the rescue
Redis is key-value store, it is similar to memcached in many ways, especially being super fast and very suitable for caching, it also have tons of extra features that makes it really interesting.

The feature that caught my eyes is “Publish/Subscribe”, basically a client would “publish” a message to a given channel, and all other client who “subscribed” to the same channel would receive the message, if no one “subscribed” no harm is done and the publishing succeeded, if multiple client “subscribed” they will all get a copy of the message.

The plan was to have the CMS (written in PHP) publish a message with a name of any file changed, and fast_sync daemon (written in Python) would collect these files and copy them individually using “rsync” to the remote server, instead of syncing a whole directory.

faster file system syncing of course … you forgot the title already!
Seriously, the file syncing now takes few seconds, instead of couple of minutes or more, we could also do image thumbnail generation before syncing inside fast_sync daemon, and if everything fails we still have cron+rsync

From CMS in PHP, this should be integrated into the CMS file manipulation code:

< ?php
$r = new Redis();
$r->connect('', 6379, 0.5);
$r->publish("new_files", "/path/to/www/htdocs/img/test.jpg");

“fast_sync” in “Python”, this should run all the time, may be start it using “nohup &”

#!/usr/local/bin/python -u

import redis
import time
import thread
import os

files = []
lock = thread.allocate_lock()

def send_files():
    global files, lock

    while 1:
        my_files = list(files)
        del files[:]
        if len(my_files) > 0:
            command="rsync -azv %s www:/path/to/www" % (" ".join(my_files))


def main():
    global files, lock, base

    # Start file sending thread
    thread.start_new_thread(send_files, ())

    # Start redis queue listener
    files = []
    r = redis.Redis(host='localhost', port=6379, db=0)
    for msg in r.listen():
        # add file to sending queue
        if msg["type"] != "message": continue
        file = msg["data"]
        print "LISTENER: Adding ", file

if __name__ == "__main__":
    try: main()
    except KeyboardInterrupt: print "Stopped by user"

Django 1.0 released!

Django team released version 1.0 of the their web application framework, Django is written in Python.

I only tried Django for a week more than one year ago, and it was really amazing, and easy to create web application.

Even if you don’t write in Python I recommend you spend sometime to know it, may be walk through the tutorial. It has many inspiring and brilliant features you definitely need to know before select your next web framework.

Wikipedia Page