python - Threading in a django app -


i'm trying create call scrobbler. task read delicious user queue, fetch of bookmarks , put them in bookmarks queue. should go through queue, parsing , store data in database.

this calls threading because of time spent waiting delicious respond , bookmarked websites respond , passed through api's , silly wait that.

however having trouble threading , keep getting strange errors database tables not being defined. appreciated :)

here's relevant code:

# relevant model # class bookmark(models.model):  account = models.foreignkey( delicious )  url = models.charfield( max_length=4096 )  tags = models.textfield()  hash = models.charfield( max_length=32 )  meta = models.charfield( max_length=32 )  # bookmark queue reading # def scrobble_bookmark(account):  try:   bookmark = bookmark.objects.all()[0]  except bookmark.doesnotexist:   return false   bookmark.delete()   tags = bookmark.tags.split(' ')  user = bookmark.account.user   concept in concepts.extract( bookmark.url ):   tag in tags:    concepts.relate( user, concept['name'], tag )   return true  def scrobble_bookmarks(account):  semaphore = semaphore(10)  in xrange(bookmark.objects.count()):   thread = bookmark_scrobble(account, semaphore)   thread.start()  class bookmark_scrobble(thread):  def __init__(self, account, semaphore):   thread.__init__(self)   self.account = account   self.semaphore = semaphore   def run(self):   self.semaphore.acquire()   try:    scrobble_bookmark(self.account)   finally:    self.semaphore.release() 

this error get:

exception in thread thread-65: traceback (most recent call last):   file "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner     self.run()   file     "/home/swizec/documents/trees/bookmarklet_server/../bookmarklet_server/scrobbler/scrobbler.py", line 60, in run     scrobble_bookmark(self.account)   file     "/home/swizec/documents/trees/bookmarklet_server/../bookmarklet_server/scrobbler/scrobbler.py", line 28, in scrobble_bookmark     bookmark = bookmark.objects.all()[0]   file "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 152, in __getitem__     return list(qs)[0]   file "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 76, in __len__ self._result_cache.extend(list(self._iter))   file "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 231, in iterator     row in self.query.results_iter():   file "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 281, in results_iter     rows in self.execute_sql(multi):   file "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 2373, in execute_sql cursor.execute(sql, params)   file "/usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py", line 193, in execute     return database.cursor.execute(self, query, params) operationalerror: no such table: scrobbler_bookmark 

ps: other tests depending on same table pass flying colours.

you can not use threading in memory databases (sqlite3 in case) in django see bug. might work postgresql or mysql.

i'd recommend celeryd instead of threads, message queues easier work threading.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -