Archive

Archive for the ‘Django’ Category

Revisiting long running processes in Django

March 2nd, 2009 michael 6 comments

So in my last post on this topic I discussed different ways to handle the problem of running code that wold otherwise cripple your web server, by pushing them off to another process.

It was mentioned that there may be other ways to attack the problem, either through Python threads or Django’s signals. This post is a review of those two suggestions.


Django’s signals

I’ll attack the simpler option first, since the response to whether this solution is viable takes all of one word: no. Signals are not asynchronous by nature and therefore they do not work as a solution to the problem. It is true that you can setup signals to work asynchronously, but you’d have to employ one of the methods discussed to handle that. So if you were to use signals to handle some asynchronous work then all they would really be doing is changing the layout of that code. You’d still need something else to actually handle the asynchronous work.

I’ll offer up a simple example of how signal code looks just for completeness.

There are three pieces to signals.
1.The signal itself
2.The sending of the signal
3.The listeners of the signal

1. Setting up the signal is as simple as including a signals.py file in your application.

import django.dispatch
test_sig = django.dispatch.Signal()

2. All you need to do to send out the request to all the listeners is call “send” off the signal. In the following code snippet I’m calling the send from one of the view functions.

from django.shortcuts import render_to_response
from django.template import RequestContext
from my_project.my_app import signals
def home(request):
    signals.test_sig.send(sender=None)
    return render_to_response('home.html',
                              context_instance=RequestContext(request),)

3. To setup a listener you call the connect function on the signal and to have link the listener to an instance method or function. Beware: the connect call has to come after the function definition. This code can really sit anywhere within your project.

def signal_func(*args, **kwargs):
    print "Do something here"
test_sig.connect(receiver=signal_func)

Python’s threads

(Update: See Malcom’s comments below, which kind of nullify the positive things I wrote about threads and this problem.)

Threads are actually a very fitting way to handle this scenario. Of course the thought of using threads did cross my mind when first attacking this problem, but it’s the kind of thing I’ve ignored as an option because I know you can get yourself in trouble when you start fiddling with an animal like this, but at the same time they are very powerful and useful if used properly.

It’s really a very simple way to resolve this problem, just push off the work to another thread within the same Python application. And in Python making use of threads is actually quite simple.

Here’s a simple intro into Python threads. There are two levels of thread libraries. The lower level object is called ‘thread’ and the higher level threading module is call ‘threading’. In most cases you will probably be working with the ‘threading’ module.

Another thing to know about is the GIL (Global interpreter lock). Since Python is an interpreted language and threads seem to all be working at the same time, there has to be a way to insure that they are not both using the same objects at the same time. The GIL locks the interpreter so that each thread can safely use Python’s internal data structures. The lock keeps moving between threads pretty frequently (I think it’s something like every 100 bytes).

Important functions to know about from threading:
__init__: initializes thread
start: starts the thread
run: code that actually runs when thread is activated
join: when called waits for thread to finish before continuing

A very simple example:

from django.shortcuts import render_to_response
from django.template import RequestContext
import threading

class TestThread(threading.Thread):
    def run(self):
        print "%s starts" % (self.getName(),)
        import time
        time.sleep(5)
        print "%s ends" % (self.getName(),)

def threadView(request):
    testThread = TestThread()
    testThread.start()
#    testThread.join() # if you remove the first pound sign on this line the becomes synchronous.
    print "Prints right when requested just to show that the other thread is off on it's own"

    return render_to_response('test.html',
                              context_instance=RequestContext(request),)
Categories: Django, Python Tags:

Running long processes in Django

February 2nd, 2009 michael 5 comments

My original issue was that I had this piece of code in my Django project that was taking way too long to run, sometimes even leading to a load time out. The particular situation happens very infrequently (at most once a week), so it was just a matter of getting the process to run successfully. Along the way, though, I learned a lot about spawning processes, distributed computing, and the different options we have in the Python community. The different approaches are just ways to get the processing to be done outside the Django process, of course.


cron and queue

cron
I will first start with the recommended process of taking care of this issue. You can setup a cron job to run some code that checks every minute to see if there’s anything to process and then run the appropriate code. Cron jobs are pretty simple to setup and pretty effective. All you need to do is edit the crontab with the time intervals you want the service to be run and your code takes care of the rest.

django-queue-service
The cron part of this solution takes care of when the processing happens, but what handles why it happens? So for that aspect of it you’ll need some way to know when there is processing to be done. There are of course multiple ways to handle this. Update a table in your database, update a file, or a folder… One way is to use django-queue-service. This method requires you to run the queue service as another django instance and then make requests to it. The sample code from the projects page looks as such:

import httplib2
client = httplib2.Http()
body_string = "message=%s" % (message_to_be_inserted_in_queue,)
uri = 'http://dqshost:8000/q/queue_name_here/put'
(response,content) =client.request(uri=uri,
method='POST',
body=body_string)
if response['status'] != 200:
print "Queue didn't accept the input"

While this method does make the most use of Django of all the methods I’ll discuss, I really have problems with it. It’s heavy handed, unnecessary, and I can’t even tell if there are security concerns. Let’s say that someone set this method up improperly and exposed this django instance to the outside world…

queue: Python module
There was some Python module I came across, which I can’t seem to find. When I find it I’ll post a link, but the way it worked is that it was a file based queue. It would add files to folders. And there were five different folders based on the status of the item of the queue. Ready, active, complete… This was a better way to handle the queue than the django-queue in my opinion, but still seemed a bit uncomfortable.


Read more…

Categories: Django, Python Tags:

Pinax update: breaks external dependencies

February 2nd, 2009 michael 5 comments

Have you been working off the Pinax trunk only to be surprised by the following message after an update:
 Error: No module named notification

Basically what happened is summarized on the django-hotclub page, which I apparently should start paying closer attention to:
http://code.google.com/p/django-hotclub/wiki/MovingToDistutils

What this all means is that you will need to change your setup a bit to get Pinax to work in its new setup… What I had to do first was install pip and virtualenv
sudo easy_install pip
sudo easy_install virtualenv

If you need to setup easy_install or you have any trouble with the following step, try updating python-setuptools
sudo apt-get install python-setuptools

You will also need to install git and bzr as mentioned in the wiki page. On my Ubuntu box, it was simple as installing git-core and bzr
sudo apt-get install git-core
sudo apt-get install bzr

After that you need to run the following command to download the apps (Feb 6, typo fixed, thanks Vernon):
sudo pip install -r pinax/requirements/external_apps.txt

And then back to your normal:
python manage.py syncdb
python manage.py runserver

(of course this is just on your dev box…)

hope that helps.

Categories: Pinax, Ubuntu Tags:

Initial DB data

September 2nd, 2008 michael No comments

Within your application on the same level as your models place a folder named “fixtures”

In my current setup that means “app/models/fixtures/”, but in a normal setup that would be “app/fixtures/”

Within that file all you need to do is add a file with the extension “xml” or “json”. The syntax looks a lot nicer for “json”:

[
{
"model": "app_name.model_name",
"pk": 1,
"fields": {
"name": "A",
"value": 5
}
},
{
"model": "app_name.model_name",
"pk": 3,
"fields": {
"name": "C",
"value": 15
}
}
]

When you’re all set with your data all you need to do is run “python manage.py loaddata json_filename” (leaving off the “json” extension)

Categories: Django Tags:

Code bloat in your Django projects: Models

August 28th, 2008 michael No comments

My current project’s getting a bit large, so I figured I should start subdividing the code a bit. The project’s already split up into multiple applications, but my main application is growing…

The first thing I’ve done is split up my models.py into multiple files and moved them into a folder named models.

Folder structure is: project/application/models/model_file.py

All that’s left to be done is to add an _init_.py to that folder and import all your models within the __init__.py. You’ll also need to make sure the app_label set on each of your models.

For example:
class Shape(models.Model):
value = models.CharField ()
class Meta:
app_label = "my_project"

This may turn into a series…lets see if there’s anything else that needs better organization.

Categories: Django Tags:

Django: messaging

August 5th, 2008 michael 3 comments

When completing something like deleting an item or adding a user, it’s usually nice to report a message to the user to let them know what just happened. I also find it useful to send the user somewhere they’d “want” to go. For example, if they just created a user, send them to the list of users page, so they see the change they just made.

I’m also a big advocate of keeping URLs clean.

Because of these concerns the best answer in Django is to do an HttpResponseRedirect. Now the problem with that is that you can’t pass data to a redirect. To get around this there are messages that you can send to the users session itself.

The mechanism isn’t that clean, but it does work, and it does do the job.

So if you have a logged in user all you need to do to add messages is:
request.user.message_set.create(message='User successfully created.')

Now the flip side to the equation is that you “get” the message(s). So how this works is when you set a message you’re actually adding a message to a list. When you”get” the message, you’re actually getting the list (may be more than one message). And if you don’t “get” the message at the right time, the messages will just build up. But for the most part this system works just fine. To get the messages you just do:
messages = request.user.get_and_delete_messages()

Then just pass ‘messages’ to the view and show the list to the user.

Categories: Django Tags:

Localization options in Django

April 16th, 2008 michael No comments

Was having trouble finding the language abbreviations everyone was using with localization. Apparently it’s a standard: ISO 639-1

Also when setting up localization you need to create a ‘locale’ folder. The location is your choice.
Then add one by one the locales from the same level as where your locale folder is. So if ‘locale’ is in app
app/$ django/bin/make-messages.py -l en

To update the ‘.po’ files later run:
app/$ django/bin/make-messages.py -a

To build the object files (‘.mo’) run:
app/$ django/bin/compile-messages.py

Categories: Django Tags:

Django through Apache on Ubuntu

March 14th, 2008 michael No comments

To: /etc/apache2/httpd.conf
Add:

<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE <project>.settings
PythonDebug On
PythonPath "['/parent', '/parent/project'] + sys.path"
</Location>

I found the instructions a bit confusing on the site. So I hope this clears up things a bit. My thought is they think the setup for live systems and where those files reside is different in the minds of the developers although that pathway isn’t really brought to light in the docs. It’s kind of hinted at, but here it goes.

In order to get my project up on Apache I had to include both the path for my project as well as its parent. The reason being is that it needs to be able to tie into your settings.py file and wants to know the project that’s a part of. In order to get that relationship it needs the folder of the project itself. I also added the path for the project so that it could tie into the applications.

What this means.
Replace <project>: with the name of your project
Replace /parent:  with the directory of the parent directory to your project
Replace /parent/project: with the directory of your project
Save the file and then restart apache:

apache2ctl restart

Categories: Apache, Django, Ubuntu Tags:

Installing Django from SVN and setting up django-registration (on Ubuntu)

March 14th, 2008 michael No comments

Go to the shell and navigate to a directory that you want to leave the Django codebase. then enter the following code to check out the current codebase from Django:
svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk

Next you will need to make a symbolic link to the Django codebase:

sudo ln -s `pwd`/django-trunk/django/ /usr/lib/python2.5/site-packages/django

Link to django-admin, which is used to create Django projects:

sudo ln -s `pwd`/django-trunk/django/bin/django-admin.py /usr/local/bin

Make sure not to use regular apostrophes around pwd or else you could run into an error with your libraries.  I was getting this error (since the mapping was going to the wrong place)
Traceback (most recent call last):

File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named django.core.management

Django registration

svn checkout http://django-registration.googlecode.com/svn/trunk/registration/ registration/
sudo ln -s `pwd`/registration /usr/lib/python2.5/site-packages/registration

Categories: Django, Linux, MSSQL, Ubuntu Tags:

Accepting NULL value in URL

March 10th, 2008 michael No comments

Within my Django project I wanted to have a page within the site for example “/page”. For that site I want there to be an optional value the page can take. So any of the following examples are acceptable: “/page/1″, “/page/2″, “/page”

There are two ways to do this.

First way (which is probably the right way) is to use the urls.py to handle this logic, like this:


(r'^pages/$', 'my_messages', {'page': page}),
(r'^pages/(\d*)$', 'my_messages'),

The other way is to handle it within the view with the following check:

if page == u'':

Categories: Django Tags: