Blog

Creating a Django cron job

I couldn’t find a way to directly call a Python function in a Django application view from the command line. It doesn’t seem like it is a common thing to do from my Google search attempts. In this example I have a function defined to download a few web sites once a day using a custom made Django model. The project is called mytestproject and the application is called mytestapp. Here is the views.py file:

import urllib2
from models import WebSite
def daily_job():
    for site in WebSite.objects.all():
        page_html = urllib2.urlopen(site.url).read()
        do_something_with_page_html(page_html)

To run this function from the command line an optimist would create a python script that looks like this:

#!/usr/bin/env python
from mytestapp.views import daily_job
daily_job()

Running this will give you an exception about your DJANGO_SETTINGS_MODULE environment variable not being defined:

EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Lets change the script a little to conform with Django’s demands.

#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from mytestapp.views import daily_job
daily_job()

Please note that according to the documentation, DJANGO_SETTINGS_MODULE should be ‘mytestproject.settings’ instead of just ‘settings’. Using the project name in DJANGO_SETTINGS_MODULE will cause troubles in our situation because Django does some tricky things to your path before importing the settings module. For our needs it isn’t necessary to do this.

Of course you can make this script a little more generic so you can run an arbitrary script from your cron job if you feel the need to:

import sys
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

module_name = sys.argv[1]
function_name = ' '.join(sys.argv[2:])

exec('import %s' % module_name)
exec('%s.%s' % (module_name, function_name))

I’ll call this script run.py. To run my daily_job function:

python run.py mytestapp.views daily_job()

And your modnight cron job entry should look something like:

0 0 * * * python /path/to/mytestproject/run.py mytestapp.views daily_job()

And there you have it. I hope this will save you some time!

Python Quake 3 server rcon and query class

Once again, I couldn’t find a Quake 3 server query/rcon class for Python so I made one. This one is substantially better then the PHP Quake 3 rcon class that I made a few days ago. It’s called pyquake3. The features are:
* Simple interface
* Automatic retries
* Can access server variables
* Can send rcon commands
* Can collect player names, ping and frags.
* With an rcon password, can collect player ip addresses.

The Python Quake 3 project page is here.

A fast Python vector class

Since completing the first version of Roids, I decided to get right into optimisation. After running the Python profiler, I noticed my vector class was taking a long time in __getitem__, so I did some tests. It turns out that __getitem__ is extremely slow compared to direct attribute access. For example in some cases I was using pos[0], pos[1] instead of pos.x, pos.y. By using attribute access, it yielded approximately an 8-10 time improvement over __getitem__. I quickly removed any reference to this!

I became curious about what else I can improve, I tested the time difference between creating an old-style class and a new-style class. It turns out that new-style classes are created 1.5x faster then old-style classes.

An Old-style class looks like this:

class Vec:
    def __init__(self, x, y):
        self.x = x
        self.y = y

New-style classes look like:

class Vec(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

There was another test I did, which was using __slots__. By defining a list of attributes you will be using in your class, a new-style class will not create a __dict__ object for the class. This decreases the time it takes to create an instance of the class by a slight amount (13.6% in my tests). Here is an example:

class Vec(object):
    __slots__ = ('x','y')
    def __init__(self, x, y):
        self.x = x
        self.y = y

One final test was using a list as a base class instead of just using object. For example:

class Vec(list):
    def __mul__(self, o):
        return vec((self[0] * o, self[1] * o))
a = vec((1.0,2.5))

This was about 20% slower then my new-style class using __slot__.

Overall these changes improved the speed of Roids quite substantially. After all that, here is the fast Python vector class.

Note: These tests were run on a Pentium 4 running Windows XP with Python 2.4.2. Since I’m only testing on one platform these results may vary for you. I plan on testing these on other computers and operating systems soon.

40 hour python multiplayer game challenge

After 6 days and 40 hours I managed to make a simple multiplayer game called Roids. On the surface it looks like another Asteroids clone but it is a litte more then that. At the moment Roids has two teams with players that fly around just like the original Asteroids, but one player on each team can become a RTS player. It was written in Python so it runs a little slow and will be improved over time. The version released was 0.0.1 and I’ll be working on it for the next few weeks.