django - signals

Posted by Ian Holsman Wed, 14 Jun 2006 03:29:44 GMT

continuing the recent thread about contenttypes in django I thought I would talk about a feature which got added in the magic removal branch, which doesn’t have as much attention as I think it deserves.

signals and the dispatcher.

signals are way of telling the rest of the world that something happened. If you are interested you simply listen for it (connect in django speak).

take for example my tagging application currently in use on zyons. one of it’s features is that it let’s users store their own tags.

One of the performance improvements I added to this was the creation of a ‘summary’ tag which aggregates which the users preferences into a single record.

Now, the first approach I could have taken was to call a ‘generate_summary_tag’ function every time I modify the user tag, but that was just messy, and it would be quite possible that I would forget somewhere.

Instead I did the following in the models.py:

dispatcher.connect( increment_tag_summary , signal=signals.pre_save, sender=TagUserObject )
dispatcher.connect( decrement_tag_summary , signal=signals.post_delete, sender=TagUserObject )

Now.. every time the django ORM updates a TagUserObject record my function will get called.

Other examples in the zyons code base include using signals to update the forum and conversation models to show the last-comment date and the number of posts. (instead of looking them up).

But you don’t need to only use django’s pre-defined signals. you can create your own.

For example, in my counter application (which is used to determine ‘popular’ conversations in the forums) uses a custom signal (object_viewed) to do it’s work.

Whenever a user views a forum or a conversation a object_view signal is sent. ala

   dispatcher.send(signal=signals.object_viewed,  request=request, object = object )

At the moment I’m doing the heavy lifting at request time, but there is nothing stopping me just changing the logic of ‘increment_tag_summary’ to use ActiveMQ via Stomp and having a seperate batch job do it instead.

Other uses of the pre_save signal that I plan to do in the near future is to update a SolR lucene-based search server and use it instead of some complex/heavy MySQL that is currently done, by creating a ‘de-normalised’ version of some of the records and sticking it in SolR.

Oh… and a request.. zyons.com is looking for a new home. If you can provide a mod-python, mysql and shell access it would be appreciated.. my home machine (a dual pentium II 450) is beginning to show it’s age.

Posted in  | Tags , , , ,  | 1 comment | no trackbacks

Looks like STOMP has some competition

Posted by Ian Holsman Mon, 03 Oct 2005 14:00:00 GMT

IBM has released ’XMS’ which allows you to talk to a JMS server from ‘c’ or .net.

Of course.. in order to play you need a copy of Websphere MQ.

so in reality STOMP is quite safe as it is a open protocol, with already 2 different server implementations.

If they could convince another of the OSS JMS vendors to implement STOMP as well they might be on their way of becoming a de-facto standard, and I wouldn’t have to worry about which server I’m using.. just like the java guys don’t have to.

Posted in  | Tags , , , ,  | no comments | no trackbacks

Perl Client to ActiveMQ (upgrade)

Posted by Ian Holsman Mon, 29 Aug 2005 22:14:00 GMT

This upgrade fixes a nasty bug where the client would garble incoming messages on a busy/slow connection.

Perl Interface to the popular Active MQ Message Queueing System, which has the download tarball on it.

Posted in  | Tags , ,  | no comments | no trackbacks

Perl Client to ActiveMQ

Posted by Ian Holsman Mon, 22 Aug 2005 19:40:00 GMT

Thanks to the hard work of Brian McCallister and his documentation of the Stomp Protocol.

I have written a perl interface which can connect and send/recieve messages to a Active MQ Message queue.

Alpha quality code is here, and a little blurb is available on Perl Interface to ActiveMQ.

Once it is tested a bit more, it will live on CPAN I guess.

Comments/Bugfixes/flames are welcome.

Posted in  | Tags , ,  | no comments | no trackbacks