I have a normal Django site running. In addition, there is another twisted process, which listens for Jabber presence notifications and updates the Django DB using Django's ORM.
So far it works as I just call the corresponding Django models (after having set up the settings environment correctly). This, however, blocks the Twisted app, which is not what I want.
As I'm new to twisted I don't know, what the best way would be to access the Django DB (via its ORM) in a non-blocking way using deferreds.
- deferredGenerator ?
- twisted.enterprise.adbapi ? (circumvent the ORM?)
- ???
If the presence message is parsed I want to save in the Django DB that the user with jid_str is online/offline (using the Django model UserProfile
). I do it with that function:
def django_useravailable(jid_str, user_available): try: userhost = jid.JID(jid_str).userhost() user = UserProfile.objects.get(im_jabber_name=userhost) user.im_jabber_online = user_available user.save() return jid_str, user_available except Exception, e: print e raise jid_str, user_available,e
Currently, I invoke it with:
d = threads.deferToThread(django_useravailable, from_attr, user_available)
d.addCallback(self.success)
d.addErrback(self.failure)