4

I have multiple db table that map to different django models. Now I wish to query them parallely maybe using threads. For example:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True);

    isATutor = models.BooleanField();
    timeAvailable = models.CharField(max_length=3);

class TutorProfile(models.Model):
    user = models.ForeignKey(User);

    language = models.CharField(max_length=30);
    unique_together = (("user", "language"), );

class Tutor(models.Model):
    user = models.ForeignKey(User);

    subject = models.CharField(max_length=30);
    unique_together = (("user", "subject"), );

Now, say I want to query UserProfile table for timeAvailable field and TutorProfile table for language field and Tutor table for subject field using threads. So how do I do it?

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 2
    Why do you want to do that? And what makes you think threads are the answer? – Daniel Roseman Sep 22 '12 at 07:59
  • Why do you need to query them in parallel? – demalexx Sep 22 '12 at 07:59
  • @user1690465 Do You want ask for data separately? for example: One thread gets UserProfiles, second TutorProfiles and 3rd Tutors ? – WBAR Sep 22 '12 at 10:04
  • What is the problem that you are trying to solve? – Burhan Khalid Sep 22 '12 at 10:23
  • I have several tables and want to perform a complex calculation based on various data fetched from the database. Moreover, I want to make my system as real time as possible. So, say, I want to add two numbers stored in 2 different dbtables and I want to add them. So i want to fetch them simultaneously and then do the operation on the numbers fetched. This is a very naive example of the application that I want to build. – user1690465 Sep 24 '12 at 03:47
  • P.S. The application I am building is a web application within django framework. – user1690465 Sep 24 '12 at 03:48

1 Answers1

1

This is only a scratch but maybe will help:

import threading
class PararellThread(threading.Thread):
     def __init__(self,model):
         threading.Thread.__init__(self)
         self.model = model
         self.result = []

     def run(self):
         self.result = self.model.objects.all()

def get_objects_in_pararell( models ):
    threads = []
    result = []
    for model in models:
        t = PararellThread(model)
        t.start()
        threads.append(t)

    for thread in threads:
        thread.join()
        for obj in thread.result:
             result.append(obj)

    return result
WBAR
  • 4,924
  • 7
  • 47
  • 81
  • I have several tables and want to perform a complex calculation based on various data fetched from the database. Moreover, I want to make my system as real time as possible. So, say, I want to add two numbers stored in 2 different dbtables and I want to add them. So i want to fetch them simultaneously and then do the operation on the numbers fetched. This is a very naive example of the application that I want to build. – user1690465 Sep 24 '12 at 03:46
  • Have You heard about database views? Look at http://stackoverflow.com/questions/1041855/use-database-view-in-django how to manage them in django – WBAR Sep 24 '12 at 07:37