In web applications sometimes you want to run processes that are seperated from the Request/Response cycle, like scheduled jobs that run at a specific time of a day. Scheduling such jobs in a unix environment is very easy with cronjobs. You just need to edit the crontab file and define a time and a command(a script to be run in this case) to execute at that time.
This post is about how to schedule such Django programs, that is python programs that import and use the Django framework.
This should be trivial because a Django program is another python program. But there is an important step to be done in about for Django modules to be discovered and run. That is to specify the Django settings file.
Let’s say there is a Django project called mysite and a Django app called myapp that has a method called runJob() in a python module called jobs.py that needs to be called every 30 mins.
let’s write a python script called thescript.py that will run every 30 minutes as a cronjob that will in turn call the runjob method.
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘settings’
from myapp.jobs import runJob
For easiness save the ‘thescript.py’ in the mysite folder.
Then open a console and enter crontab -e to edit your cron file. Enter the following line.
*/30 * * * * python /path to mysite/thescript.py > logfile 2>&1
This will run the ‘thescript.py’ file every 30 minutes and any output would be directed to the logfile specified and the last ‘2>&1’ tells that no email should be sent to the administrator. For more information on how to write the crontab file have a look at this.
That is how easy it is to schedule a Django cronjob in an Unix environment.
Django is the most widely used if not the most popular web framework among those available for python. Have a look at this report by Jacob Kaplan-Moss the co-creator of Django, to get an idea about the current standing of Django among the community.
One disadvantage for Django is the lack of IDE support it has. Here it holds a clear disadvantage against it’s rival web frameworks such as Ruby on Rails. Wing IDE looks impressive but is not free. I wasn’t much impressed with Komodo and it is not free either. The best bet seams to be Netbeans 7 which promises to have good python support. Hopefully it would have satisfactory Django support as well. Currently netbeans is having a python plugin that is still in beta stage. The best alternative for me as of now is PyDev which I have been using with python for sometime now. Here I will explain how to configure PyDev to work with Django.
1.) First download and install Django
2.) Add Django source folder to PYTHONPATH in pydev
- Go to windows > preferences
- In the opended dialog choose interpreter-python
- Here under python interpreter a python interpreter should be available. If not add one by clicking new
- In System PYTHONPATH add the downloaded source folder of Django by clicking New Folder
3.) Create a new pydev project.
- Go to file > new > pydev project
- Enter project name, path and importantly make sure the option ‘create default src folder and add it to PYTHONPATH?’ is choosen. This is easiest way or you can manually add the source folder of the new project to PYTHONPATH by going to File > properties > PYTHONPATH
4.) Start a new Django project
- Open a new console from the Operating System(console support inside pydev is avialable but is not very satisfactory)
- Navigate to the newly create src folder inside the pydev project
- type django-admin.py startproject ‘sitename’ (sitename should be the name of your project). This how a new project is started in Django
If your projects name is mysite the folder structure should be as follows
Now pydev should be configured to work with Django. All the python feature that pydev supports should be now working with Django as well.
The standard features of a modern IDE works beautifully with pydev for Django but there are some major drawbacks. Django template tags are not supported in pydev. Also the console support inside pydev is not impressive. So for now I am forced to use pydev for Django coding, the OS console for giving Django commands and nothing for Django templates.
Python has a simple yet powerful builtin module for debugging called pdb. Using this module you can debug your python code easily even without any IDE.
1. First step is to import the pdb module
2. Next step is to mark the entry point where the debugger would be called in. We use the set_trace() method for that.
3. When the program is run the execution would stop at the set_trace() method and would wait for debugging commands.
4. To run the next statment enter ‘n’ or ‘next’
5. To print a variable enter ‘pp variable_name’
6. To run until exit enter ‘c’ or ‘continue’
7. To display the current location enter ‘l’ or ‘list’
This would display an appropiate part of the source code with a pointer to the current statement.
8. To step into a method enter ‘s’ or ‘step’ when debugger is met with a method
9. To insert a break point enter ‘b filename:linenumber’
10. You can also dynamically assign values into variables.
Let’s assume there is a variable called num initialized to 10. You want to see what would happen when that variable is initialized to 20. You just do this. (pdb) num = 20 and then enter ‘n’ to execute next statement or enter ‘c’ to execute program until the end.
An example would look like this
i = 10
name = ‘kasun’
last_letter = name[-1]
last_num = ord(last_letter)
print ‘last_num is’, last_num
if __name__ == ‘__main__’:
As I said earlier I am in the process of moving into python. I am in love with the beast at the moment and thought I would write down what I find interesting in python. Pros and cons, both. This is my journey into Python.
The first thing that I found of interest is the python interactive shell. It allows you to run python statements and expressions in the console. This is really handy when you are learning python or testing a python module. How many times have you skipped checking out some feature while learning a language like Java because writing a whole class and then running it to test a small feature is not worth all the trouble? I do it all the time. That is where the python interactive shell comes in.
python interactive shell
Importing modules, instantiating variables and running statements can be done right in the console by using the python interactive shell.